In C#, File.Create() creates a new empty file and gives you a FileStream that is open for reading and writing. If the file already exists, it is emptied first.
The returned stream locks the file until you close it, so always wrap the call in a using block. To save text in one line of code, WriteAllText is easier; Create is for when you need the stream.
This is a static method of System.IO.File. Call it as File.Create(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Call it inside a using statement so the stream is closed and the file is released.
Creates the file, or empties it, and returns a read and write FileStream.
File.Create(path, bufferSize)
Same, with your own buffer size in bytes. The default is 4096.
File.Create(path, bufferSize, options)
Adds FileOptions such as WriteThrough or DeleteOnClose.
File.Create returns a FileStream, which deals in bytes. To write text lines, wrap it in a StreamWriter, or call File.CreateText to get one directly.
Library declarations
C#
public static FileStream Create(string path);
public static FileStream Create(string path, int bufferSize);
public static FileStream Create(string path, int bufferSize, FileOptions options);
Parameters
Parameter
Description
path
The file to create. Its folder must exist. An existing file is truncated to zero length.
bufferSize
Optional. How many bytes the stream buffers before it writes to disk. It must be greater than zero.
options
Optional. FileOptions flags such as Asynchronous, WriteThrough, or DeleteOnClose.
Return Value
Return Value
Description
FileStream
A stream opened with read and write access and no sharing (FileShare.None). You must close or dispose it.
C# Examples
Example 1 — Create a File and Write Bytes to It
Create an empty file, write five bytes, and check the length before and after.
C#
using System;
using System.IO;
using System.Text;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-create");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "hello.bin");
byte[] data = Encoding.UTF8.GetBytes("Hello");
using (FileStream fs = File.Create(path)) {
Console.WriteLine(fs.Length);
fs.Write(data, 0, data.Length);
}
Console.WriteLine(new FileInfo(path).Length);
Directory.Delete(root, true);
}
}
Output
0
5
How It Works
A new file is empty. Right after File.Create the stream length is 0.
Text becomes bytes.Encoding.UTF8.GetBytes("Hello") gives five bytes, because every letter in Hello takes one byte in UTF-8.
Write, then close.fs.Write sends the bytes into the file. The closing brace of the using block disposes the stream and releases the file.
Measure from outside. A fresh FileInfo reports a length of 5.
Example 2 — Create Empties an Existing File
Call File.Create on a file that already has content and see what is left.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-create-empty");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "data.txt");
File.WriteAllText(path, "123456789");
Console.WriteLine(new FileInfo(path).Length);
using (File.Create(path)) { }
Console.WriteLine(new FileInfo(path).Length);
Console.WriteLine(File.Exists(path));
Directory.Delete(root, true);
}
}
Output
9
0
True
How It Works
Nine bytes to start. The first line prints 9, the size of the nine digits saved by WriteAllText.
An empty using block.using (File.Create(path)) { } opens the file and closes it again without writing anything. The truncate happens at the open.
Content is gone, file stays. The length is now 0 but Exists still prints True. This is the trick for making an empty file.
Example 3 — Wrap the Stream in a StreamWriter
Use the FileStream that Create returns to write text lines.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-create-writer");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "lines.txt");
using (FileStream fs = File.Create(path))
using (StreamWriter writer = new StreamWriter(fs)) {
writer.WriteLine("line 1");
writer.WriteLine("line 2");
}
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[0]);
Directory.Delete(root, true);
}
}
Output
2
line 1
How It Works
Two layers. The FileStream talks bytes to the disk, and the StreamWriter on top of it turns text into bytes. Two using statements without braces stack neatly.
Writer closes first. Disposing the StreamWriter flushes its text into the stream and also closes the underlying stream, so the data is on disk when the block ends.
Read it back.ReadAllLines returns two lines, and the first is line 1.
Frequently Asked Questions
A FileStream opened for reading and writing with no sharing. You write bytes to it with Write or wrap it in a StreamWriter for text.
Yes. If the file exists, its content is removed and the length becomes zero. Use FileMode.CreateNew with File.Open if you want an error instead.
The stream keeps the file open and locked. Without a using block, other code may get an IOException, and buffered data may not reach the disk.
WriteAllText saves a whole string in one call and closes the file for you. Create gives you a stream for many writes, for binary data, or for control over buffering.
Use using (File.Create(path)) { }. The file is created empty and closed again straight away.