C# Write to File

To write text to file in C#, you can use System.IO.File.WriteAllLines() function or System.IO.File.WriteAllText() function.

Or you can use WriteLine in recursive loop to write lines to a file. It gives more control on each line to be written.

Example 1 – Write to Text File – System.IO.File.WriteAllLines()

System.IO.File.WriteAllLines() writes strings in an array, to a text file.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            string[] lines = { "Hello World.", "Welcome to C# Tutorial.", "TutorialKart." };
            try{
                System.IO.File.WriteAllLines(@"D:\lines.txt", lines);
                Console.WriteLine("Lines written to file successfully.");
            } catch(Exception err) {
                Console.WriteLine(err.Message);
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Lines written to file successfully.

D:\lines.txt

Hello World.
Welcome to C# Tutorial.
TutorialKart.

Example 2 – Write to Text File – System.IO.File.WriteAllText()

System.IO.File.WriteAllText() writes string to a text file specified by the argument passed to the function.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            string str = "Hello World. Welcome to C# Tutorial. TutorialKart.";
            try{
                System.IO.File.WriteAllText(@"D:\sample.txt", str);
                Console.WriteLine("String written to file successfully.");
            } catch(Exception err) {
                Console.WriteLine(err.Message);
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
String written to file successfully.

D:\sample.txt

Hello World. Welcome to C# Tutorial. TutorialKart.

Example 3 – Write to Text File – WriteLine in Loop

In the following example, we get StreamWriter for a file and then write each line in the string array to the file.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {

    class Program {
        static void Main(string[] args) {
            string[] lines = { "Hello World.", "Welcome to C# Tutorial.", "TutorialKart." };
            try{
                using (System.IO.StreamWriter file = 
                    new System.IO.StreamWriter(@"D:\lines.txt")) {
                    foreach (string line in lines) {
                        file.WriteLine(line);
                    }
                }
                Console.WriteLine("Lines written to file successfully.");
            } catch(Exception err) {
                Console.WriteLine(err.Message);
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
Lines written to file successfully.

D:\lines.txt

Hello World.
Welcome to C# Tutorial.
TutorialKart.

Conclusion

In this C# Tutorial, we learned to write string to a text file, or lines stored in a string array to a text file.