C# Language Writing to a file using the System.IO.File class


You can use the System.IO.File.WriteAllText function to write a string to a file.

string text = "String that will be stored in the file";
System.IO.File.WriteAllText(@"C:\MyFolder\OutputFile.txt", text);

You can also use the System.IO.File.WriteAllLines function which receives an IEnumerable<String> as the second parameter (as opposed to a single string in the previous example). This lets you write content from an array of lines.

string[] lines = { "My first string", "My second string", "and even a third string" };
System.IO.File.WriteAllLines(@"C:\MyFolder\OutputFile.txt", lines);