代码如下:
1 /// <summary> 2 /// 将数据保存为txt文件 3 /// </summary> 4 /// <param name="filename">txt文件名</param> 5 /// <param name="Content">txt文件内容</param> 6 public void SaveDataToTxt(string filename,string Content) 7 { 8 // FileStream 添加引用 using System.IO; (此处是将文件保存到E盘) 9 FileStream fs = new FileStream(@"E:" + filename + ".txt", FileMode.OpenOrCreate, FileAccess.Write); 10 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")); 11 //通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码 12 sw.Flush(); 13 sw.BaseStream.Seek(0, SeekOrigin.Begin); 14 sw.WriteLine(Content); 15 sw.Flush(); 16 sw.Close(); 17 }