12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System.IO;
- using System.Threading.Tasks;
- namespace Team.Utility
- {
- public static class FileHelper
- {
- /// <summary>
- /// 创建文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <param name="str">文件内容</param>
- public static void Save(string path, string str)
- {
- var s = path.Substring(0, path.LastIndexOf('\\'));
- Directory.CreateDirectory(s); //如果文件夹不存在就创建它
- using var fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
- var data = System.Text.Encoding.Default.GetBytes(str);
- //开始写入
- fs.Write(data, 0, data.Length);
- }
- /// <summary>
- /// 创建文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <param name="str">文件内容</param>
- public static async Task SaveAsync(string path, string str)
- {
- var s = path.Substring(0, path.LastIndexOf('\\'));
- Directory.CreateDirectory(s);//如果文件夹不存在就创建它
- using var fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
- var data = System.Text.Encoding.UTF8.GetBytes(str);
- //开始写入
- await fs.WriteAsync(data, 0, data.Length);
- }
- }
- }
|