C#写文件


    /// <summary>
    /// 创建文件
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <param name="content">文件内容</param>
    public static void Save(string path, string content)
    {
        string s = path.Substring(0, path.LastIndexOf('\\'));
        Directory.CreateDirectory(s);//如果文件夹不存在就创建它
        FileStream fs = new FileStream(
                                       path, FileMode.Create, 
                                       FileAccess.ReadWrite, 
                                       FileShare.ReadWrite
                                       );
        byte[] data = System.Text.Encoding.Default.GetBytes(content);
        //开始写入
        fs.Write(data, 0, data.Length);
        //清空缓冲区、关闭流
        fs.Flush();
        fs.Close();
    }
posted @ 2020-06-02 10:35  艾孜尔江  阅读(261)  评论(0编辑  收藏  举报