C simple reading and writing method of txt file

  • 2021-10-25 07:41:32
  • OfStack

In this paper, an example is given to describe the method of simply reading and writing txt files by C #. Share it for your reference, as follows:


//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"]); // Products S/N No.  + IMEI No. 
}
sw.Write(builder);
sw.Close();
fs.Close();
if (System.IO.File.Exists(saveFileName))
{
  System.Diagnostics.Process.Start(saveFileName); // Open this file after saving successfully 
}
//read txt
string[] allLines = File.ReadAllLines(filePath);

More readers interested in C # can check the topic of this site: "Summary of common skills in C # file operation", "Summary of thread use skills in C # programming", "Summary of C # Excel operation skills", "Summary of XML file operation skills in C #", "C # common control usage tutorial", "WinForm control usage tutorial", "C # data structure and algorithm tutorial", "C # array operation skills summary" and "C # object-oriented programming introduction tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: