C# Write to File with the File Class and StreamWriter

How to Write Strings and Text to File in C#

This tutorial describes how to write strings and text to a file with the File class and StreamWriter in C#.

Related tutorial:

1. Writing text to a file with the File class

WriteAllText Method

To write text to a file, you can use the following syntax:

File.WriteAllText(string filePath, string text);

Example:

The following example shows how to write text to a file named "sample2.txt" using WriteAllText method:

using System;
using System.IO;
 
namespace WriteToFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample2.txt";
            string filePath = Path.Combine(directory, fileName);
            //

            string text = "Line 1" + Environment.NewLine
                + "Line 2" + Environment.NewLine
                + "Line 3" + Environment.NewLine;
            File.WriteAllText(filePath, text);
        }
    }
}

WriteAllLines Method

You can use the WriteAllLines method to write strings to a file, as the following example shows:

using System.IO;
 
namespace WriteToFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample2.txt";
            string filePath = Path.Combine(directory, fileName);
            //
            string[] lines = { "Line 1""Line 2""Line 3" };
 
            File.WriteAllLines(filePath, lines);
        }
    }
}

The above program writes three lines Line 1Line2, and Line 3, to a file named sample2.txt.

AppendAllLines Method

You can append lines to a file using the AppedAllLines method, as the following example shows:

using System;
using System.IO;
 
namespace WriteToFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample2.txt";
            string filePath = Path.Combine(directory, fileName);
            //

            string text = "Line 1" + Environment.NewLine
                + "Line 2" + Environment.NewLine
                + "Line 3" + Environment.NewLine;
            File.WriteAllText(filePath, text);
            string[] lines_to_append = { "Line 4""Line 5" };
            File.AppendAllLines(filePath, lines_to_append);
        }
    }
}

The above program writes three lines to a file using the WriteAllText method and then appends two lines at the end of the file.

2. Write text to a file with StreamWriter

Writing to a File

The following example shows how to write text to a file by using the StreamWriter class:

using System.IO;
 
namespace WriteToFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            //
            string[] lines = { "Line 1""Line 2""Line 3""Line 4""Line 5" };
            //
            using (StreamWriter writer = new StreamWriter(filePath))
            {
                foreach (string line in lines)
                    writer.WriteLine(line);
            }
        }
    }
}

The above program writes five lines to a file named sample.txt.

Appending a File

The following example shows how to append the existing file:

using System.IO;
 
namespace WriteToFileExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string directory = @"D:\Programming Samples";
            string fileName = "sample.txt";
            string filePath = Path.Combine(directory, fileName);
            //
            string[] lines_to_append = { "Line 6""Line 7" };
            //
            bool append = true;
            using (StreamWriter writer = new StreamWriter(filePath, append))
            {
                foreach (string line in lines_to_append)
                    writer.WriteLine(line);
            }
        }
    }
}

The above program writes two lines at the end of the file named sample.txt.

In this tutorial, you have learned how to write strings and text to a file by using File class or StreamWriter.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables