C# Language File and Stream I/O Writing to a file using the System.IO.File class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

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


Got any C# Language Question?