In C#, File.CreateText() creates a text file and returns a StreamWriter that is ready to write. The text is saved as UTF-8 without a byte order mark. An existing file is overwritten.
A StreamWriter lets you call Write and WriteLine as many times as you like, including inside loops. Close it with a using block so everything is flushed to disk.
This is a static method of System.IO.File. Call it as File.CreateText(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Use it in a using statement, then call WriteLine on the returned writer.
Creates or overwrites the file and returns a UTF-8 StreamWriter.
writer.WriteLine(text)
Writes the text and a line break.
writer.Write(text)
Writes the text with no line break, so the next write continues on the same line.
File.CreateText is a short way to write new StreamWriter(path, false). Use File.AppendText instead when you must keep what is already in the file.
Library declarations
C#
public static StreamWriter CreateText(string path);
Parameters
Parameter
Description
path
The file to create or overwrite. Its folder must exist.
Return Value
Return Value
Description
StreamWriter
A writer that saves UTF-8 text to the file. Dispose it, normally with a using block, to flush and close the file.
C# Examples
Example 1 — Write Two Lines and Read Them Back
Create a text file with a StreamWriter and check the lines.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createtext");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "letters.txt");
using (StreamWriter writer = File.CreateText(path)) {
writer.WriteLine("alpha");
writer.WriteLine("beta");
}
foreach (string line in File.ReadAllLines(path)) {
Console.WriteLine(line);
}
Directory.Delete(root, true);
}
}
Output
alpha
beta
How It Works
A writer, not a string.CreateText hands back a StreamWriter, so you write with its own WriteLine method.
Close it with using. At the closing brace the writer is disposed. That flushes the buffered text and releases the file, so the next call can read it.
One line per call. Each WriteLine ended with a line break, so ReadAllLines returns two separate lines.
Example 2 — Calling CreateText Twice Overwrites the File
Write a file, then call CreateText again with different text.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createtext-over");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "state.txt");
using (StreamWriter first = File.CreateText(path)) {
first.Write("first run");
}
Console.WriteLine(File.ReadAllText(path));
using (StreamWriter second = File.CreateText(path)) {
second.Write("second");
}
Console.WriteLine(File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
first run
second
How It Works
Write without a line break.Write saves the text only, so the file holds first run and no newline.
The second call starts over.CreateText empties the file before the writer is returned. first run is gone, not extended.
Proof by reading. The second ReadAllText prints just second. Use AppendText if you want to keep the old content.
Example 3 — Write Formatted Rows in a Loop
Build a tiny comma separated file with a loop and count the rows.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-createtext-rows");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "scores.csv");
string[] names = { "Ann", "Bob", "Cy" };
int[] scores = { 90, 75, 82 };
using (StreamWriter writer = File.CreateText(path)) {
writer.WriteLine("name,score");
for (int i = 0; i < names.Length; i++) {
writer.WriteLine(names[i] + "," + scores[i]);
}
}
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[2]);
Directory.Delete(root, true);
}
}
Output
4
Bob,75
How It Works
Header first. The writer saves a name,score header line, then one row for each person.
A loop keeps the file open. The same writer is used for every row. This is why CreateText beats calling AppendAllText four times, which would open the file four times.
Count and pick. The header plus three rows make 4 lines. Index 2 is the third line, because counting starts at zero, which is the row Bob,75.
Frequently Asked Questions
A StreamWriter that writes UTF-8 text to the file. Use its Write and WriteLine methods, and dispose it when you are done.
Yes. The old content is removed when the writer is created. Use File.AppendText to add to the end instead.
CreateText returns a StreamWriter for text. Create returns a FileStream for bytes. You can wrap a FileStream in a StreamWriter yourself to get the same result.
No. It writes UTF-8 without a BOM. If you need a BOM or another encoding, use new StreamWriter(path, false, encoding).
The writer was probably not flushed or disposed. Put the writer in a using block, or call Flush() before the program ends.