Method of Writing Text File Content by C

  • 2021-07-13 06:07:58
  • OfStack

In this paper, an example is given to describe the method of writing text file contents by C #. Share it for your reference. The details are as follows:


private void write_txt(string str1, string str2, string str3)
{
  System.DateTime currentTime = System.DateTime.Now;
  string strYMD = currentTime.ToString("d");
  string FILE_NAME = "MyFileSend" + strYMD + ".txt";
  // Set up by date every day 1 Different file names 
  StreamWriter sr;
  if (File.Exists(FILE_NAME))
  // If the file exists , Is created File.AppendText Object 
  {
  sr = File.AppendText(FILE_NAME);
  }
  else
  // If the file does not exist , Is created File.CreateText Object 
  {
  sr = File.CreateText(FILE_NAME);
  }
  sr.WriteLine(str1 + "   " + str2 + "   " + DateTime.Now.ToString("yyyy-mm-dd hh:mm:ss") + " " + str3 + "\r\n");
  // Writes the incoming string plus time to a text file 1 Row 
  sr.Close();
}

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


Related articles: