C# 创建文件以及记录内容
C#创建文件以及写文件
-
创建文件
public static bool CreateFile(string FileName, ref string strErr) { bool flag = false; try { if (File.Exists(FileName)) File.Delete(FileName); flag = true; } catch (Exception ex) { strErr = ex.Message; } return flag; } -
写文件内容
public static bool WriteData(string FileName, string Data, ref string strErr) { bool flag = false; try { using (var fs = new FileStream(FileName, FileMode.Append, FileAccess.Write)) { using (var sw = new StreamWriter(fs)) { sw.WriteLine(Data); } } flag = true; } catch (Exception ex) { strErr = ex.Message; } return flag; }