Writing to a file using the System.IO.File class

suggest change

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);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents