Write to Text File in C#

8 June, 2019

How

There are two methods for this. The first method will keep adding on the new data to the bottom of the text file, whilst the second method will overwrite all previous things in the text file whenever new data is written.

Method 1 (Don't overwrite Any Data)

TextWriter saveData = new StreamWriter(@"C:\Path\To\TextFile.txt", true);
saveData.WriteLine("Yo Bro");
saveData.Close();

Method 2 - (Overwrite Data)

string text = "I went to the dog park.";
System.IO.File.WriteAllText(@"C:\Path\To\TextFile.txt", text);