C# File WriteAllLines() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.WriteAllLines() writes a collection of strings to a file, one string per line. It creates the file or replaces an existing one, then closes it.

It accepts a string[] or any IEnumerable<string>, such as a List<string> or a LINQ query. A line break follows every line, including the last one.

This is a static method of System.IO.File. Call it as File.WriteAllLines(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Pass the path and the lines. An array, a list, or a LINQ result all work.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-writealllines.txt");

string[] lines = { "red", "green", "blue" };
File.WriteAllLines(path, lines);

Console.WriteLine(File.ReadAllLines(path).Length);
CallWhat it does
File.WriteAllLines(path, string[])Writes each array element as a line.
File.WriteAllLines(path, IEnumerable<string>)Writes any sequence, such as a list or a query result.
File.WriteAllLines(path, lines, encoding)Same, with the encoding you choose.

The line break written is Environment.NewLine, which is \r\n on Windows and \n on Linux and macOS. ReadAllLines reads either one back.

Library declarations
C#
public static void WriteAllLines(string path, string[] contents);
public static void WriteAllLines(string path, IEnumerable<string> contents);
public static void WriteAllLines(string path, string[] contents, Encoding encoding);
public static void WriteAllLines(string path, IEnumerable<string> contents, Encoding encoding);

Parameters

ParameterDescription
pathThe file to write. It is created if missing, and its folder must exist.
contentsThe lines to write. A string[] or an IEnumerable<string>. It must not be null.
encodingOptional. Defaults to UTF-8 without BOM.

Return Value

Return ValueDescription
voidNothing is returned. The file holds one line for each element.

C# Examples

Example 1 — Write an Array as Lines

Save three strings, then read the file as lines and as one text.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealllines");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "colors.txt");
        string[] colors = { "red", "green", "blue" };

        File.WriteAllLines(path, colors);

        string[] back = File.ReadAllLines(path);
        Console.WriteLine(back.Length);
        Console.WriteLine(back[2]);
        Console.WriteLine(File.ReadAllText(path).EndsWith(Environment.NewLine, StringComparison.Ordinal));

        Directory.Delete(root, true);
    }
}

How It Works

  1. One element, one line. The array has three strings, so the file gets three lines.
  2. Read them back. ReadAllLines returns an array of three, and back[2] is blue.
  3. A final line break is written. Reading the file as one string shows that it ends with Environment.NewLine, so the check prints True. Comparing against Environment.NewLine keeps the result true on every system.

Example 2 — Write a LINQ Sequence

Build the lines with LINQ and pass the sequence straight to WriteAllLines.

C#
using System;
using System.IO;
using System.Linq;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealllines-linq");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "rows.txt");
        var rows = Enumerable.Range(1, 4).Select(i => "Row " + i);

        File.WriteAllLines(path, rows);

        string[] back = File.ReadAllLines(path);
        Console.WriteLine(back.Length);
        Console.WriteLine(back[back.Length - 1]);

        Directory.Delete(root, true);
    }
}

How It Works

  1. No array needed. rows is a lazy query. The overload for IEnumerable<string> loops over it and writes each line as it is produced.
  2. Four lines. Range(1, 4) gives the numbers 1 to 4, and Select turns each into text such as Row 3.
  3. Check the end. The last element of the array read back is Row 4.

Example 3 — Write a List, Then Replace the File

Write a list of names, then write a shorter array to the same file.

C#
using System;
using System.Collections.Generic;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writealllines-list");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "names.txt");
        List<string> names = new List<string> { "Ann", "Bob" };

        File.WriteAllLines(path, names);
        Console.WriteLine(File.ReadAllLines(path).Length);

        File.WriteAllLines(path, new[] { "Zed" });
        string[] now = File.ReadAllLines(path);
        Console.WriteLine(now.Length + " line: " + now[0]);

        Directory.Delete(root, true);
    }
}

How It Works

  1. A List works. List<string> implements IEnumerable<string>, so it goes straight into WriteAllLines. Two names give two lines.
  2. The second call replaces the file. The shorter array { "Zed" } does not add to the old lines. It takes their place.
  3. Result. The file now has one line, Zed, and nothing is left of Ann or Bob. Use AppendAllLines to keep the earlier lines.

Frequently Asked Questions

A string array or any IEnumerable<string>, such as a List<string> or a LINQ query. There are overloads that also take an Encoding.
Yes. An existing file is replaced. Use File.AppendAllLines to add lines to the end of an existing file.
Yes. Every line, including the last one, ends with Environment.NewLine.
WriteAllText saves one string exactly as you give it. WriteAllLines takes a collection and puts a line break after each element.
Environment.NewLine: \r\n on Windows and \n on Linux and macOS. File.ReadAllLines understands both.

Next: WriteAllText()

Save a string to a file in one call, creating it or replacing it.

WriteAllText() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful