本文实例讲述了C#简单读写txt文件的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//write txt StringBuilder builder = new StringBuilder(); FileStream fs = new FileStream(saveFileName, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.Default); for ( int i = 0; i < ds.Tables[0].Rows.Count; i++) { DataRow dr = ds.Tables[0].Rows[i]; builder.AppendLine(dr[ "netsn" ] + "," + dr[ "imei" ]); //产品S/N号 + IMEI号 } sw.Write(builder); sw.Close(); fs.Close(); if (System.IO.File.Exists(saveFileName)) { System.Diagnostics.Process.Start(saveFileName); //保存成功后打开此文件 } //read txt string [] allLines = File.ReadAllLines(filePath); |
希望本文所述对大家C#程序设计有所帮助。