C#写入字符串到文件或者追加字符串到文件
public static void WriteStringToFile(string fileName, string contents) { StreamWriter sWriter = null; try { FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write); sWriter = new StreamWriter(fileStream); sWriter.Write(contents); } finally { if(sWriter != null) { sWriter.Close(); } } } public static void AppendStringToFile(string fileName, string contents) { StreamWriter sWriter = null; try { FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write); sWriter = new StreamWriter(fileStream); sWriter.Write(contents); } finally { if(sWriter != null) { sWriter.Close(); } } }