File.WriteAllLines Method

Definition

Creates a new file, writes one or more strings to the file, and then closes the file.

Overloads

WriteAllLines(String, String[], Encoding)

Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

WriteAllLines(String, IEnumerable<String>, Encoding)

Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.

WriteAllLines(String, IEnumerable<String>)

Creates a new file, writes a collection of strings to the file, and then closes the file.

WriteAllLines(String, String[])

Creates a new file, write the specified string array to the file, and then closes the file.

WriteAllLines(String, String[], Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Creates a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.

public static void WriteAllLines (string path, string[] contents, System.Text.Encoding encoding);

Parameters

path
String

The file to write to.

contents
String[]

The string array to write to the file.

encoding
Encoding

An Encoding object that represents the character encoding applied to the string array.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

Either path or contents is null.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

path specified a file that is read-only.

-or-

path specified a file that is hidden.

-or-

This operation is not supported on the current platform.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

path is in an invalid format.

The caller does not have the required permission.

Examples

The following code example demonstrates the use of the WriteAllLines method to write text to a file. In this example a file is created, if it doesn't already exist, and text is added to it.

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path, Encoding.UTF8);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

Remarks

If the target file already exists, it is truncated and overwritten.

Given a string array and a file path, this method opens the specified file, writes the string array to the file using the specified encoding, and then closes the file.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteAllLines(String, IEnumerable<String>, Encoding)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.

public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable<string> contents, System.Text.Encoding encoding);

Parameters

path
String

The file to write to.

contents
IEnumerable<String>

The lines to write to the file.

encoding
Encoding

The character encoding to use.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters defined by the GetInvalidPathChars() method.

Either path, contents, or encoding is null.

path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

path exceeds the system-defined maximum length.

path is in an invalid format.

The caller does not have the required permission.

path specified a file that is read-only.

-or-

path specified a file that is hidden.

-or-

This operation is not supported on the current platform.

-or-

path is a directory.

-or-

The caller does not have the required permission.

Remarks

If the target file already exists, it is truncated and overwritten.

You can use this method to create a file that contains the following:

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WriteAllLines(String, IEnumerable<String>)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Creates a new file, writes a collection of strings to the file, and then closes the file.

public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable<string> contents);

Parameters

path
String

The file to write to.

contents
IEnumerable<String>

The lines to write to the file.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters defined by the GetInvalidPathChars() method.

Either path or contents is null.

path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

path exceeds the system-defined maximum length.

path is in an invalid format.

The caller does not have the required permission.

path specified a file that is read-only.

-or-

path specified a file that is hidden.

-or-

This operation is not supported on the current platform.

-or-

path is a directory.

-or-

The caller does not have the required permission.

Examples

The following example writes selected lines from a sample data file to a file.

using System;
using System.IO;
using System.Linq;

class Program
{
    static string dataPath = @"c:\temp\timestamps.txt";

    static void Main(string[] args)
    {
        CreateSampleFile();

        var JulyWeekends = from line in File.ReadLines(dataPath)
                           where (line.StartsWith("Saturday") ||
                           line.StartsWith("Sunday")) &
                           line.Contains("July")
                           select line;

        File.WriteAllLines(@"C:\temp\selectedDays.txt", JulyWeekends);

        var MarchMondays = from line in File.ReadLines(dataPath)
                           where line.StartsWith("Monday") &&
                           line.Contains("March")
                           select line;

        File.AppendAllLines(@"C:\temp\selectedDays.txt", MarchMondays);
    }

    static void CreateSampleFile()
    {
        DateTime TimeStamp = new DateTime(1700, 1, 1);

        using (StreamWriter sw = new StreamWriter(dataPath))
        {
            for (int i = 0; i < 500; i++)
            {
                DateTime TS1 = TimeStamp.AddYears(i);
                DateTime TS2 = TS1.AddMonths(i);
                DateTime TS3 = TS2.AddDays(i);
                sw.WriteLine(TS3.ToLongDateString());
            }
        }
    }
}

Remarks

The default behavior of the WriteAllLines(String, IEnumerable<String>) method is to write out data by using UTF-8 encoding without a byte order mark (BOM). If it is necessary to include a UTF-8 identifier, such as a byte order mark, at the beginning of a file, use the WriteAllLines(String, IEnumerable<String>, Encoding) method overload with UTF8 encoding.

If the target file already exists, it is truncated and overwritten.

You can use this method to create the contents for a collection class that takes an IEnumerable<T> in its constructor, such as a List<T>, HashSet<T>, or a SortedSet<T> class.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WriteAllLines(String, String[])

Source:
File.cs
Source:
File.cs
Source:
File.cs

Creates a new file, write the specified string array to the file, and then closes the file.

public static void WriteAllLines (string path, string[] contents);

Parameters

path
String

The file to write to.

contents
String[]

The string array to write to the file.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

Either path or contents is null.

The specified path, file name, or both exceed the system-defined maximum length.

The specified path is invalid (for example, it is on an unmapped drive).

An I/O error occurred while opening the file.

path specified a file that is read-only.

-or-

path specified a file that is hidden.

-or-

This operation is not supported on the current platform.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

path is in an invalid format.

The caller does not have the required permission.

Examples

The following code example demonstrates the use of the WriteAllLines method to write text to a file. In this example a file is created, if it doesn't already exist, and text is added to it.

using System;
using System.IO;
class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string[] createText = { "Hello", "And", "Welcome" };
            File.WriteAllLines(path, createText);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText);

        // Open the file to read from.
        string[] readText = File.ReadAllLines(path);
        foreach (string s in readText)
        {
            Console.WriteLine(s);
        }
    }
}

Remarks

If the target file already exists, it is truncated and overwritten.

The default behavior of the WriteAllLines method is to write out data using UTF-8 encoding without a byte order mark (BOM). If it is necessary to include a UTF-8 identifier, such as a byte order mark, at the beginning of a file, use the WriteAllLines(String, String[], Encoding) method overload with UTF8 encoding.

Given a string array and a file path, this method opens the specified file, writes the string array to the file, and then closes the file.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1