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.
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
Parameter
Description
path
The file to write. It is created if missing, and its folder must exist.
contents
The lines to write. A string[] or an IEnumerable<string>. It must not be null.
encoding
Optional. Defaults to UTF-8 without BOM.
Return Value
Return Value
Description
void
Nothing 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);
}
}
Output
3
blue
True
How It Works
One element, one line. The array has three strings, so the file gets three lines.
Read them back.ReadAllLines returns an array of three, and back[2] is blue.
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);
}
}
Output
4
Row 4
How It Works
No array needed.rows is a lazy query. The overload for IEnumerable<string> loops over it and writes each line as it is produced.
Four lines.Range(1, 4) gives the numbers 1 to 4, and Select turns each into text such as Row 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);
}
}
Output
2
1 line: Zed
How It Works
A List works.List<string> implements IEnumerable<string>, so it goes straight into WriteAllLines. Two names give two lines.
The second call replaces the file. The shorter array { "Zed" } does not add to the old lines. It takes their place.
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.