In C#, File.Open() opens a file and returns a FileStream. It is the flexible option: you choose a FileMode (what to do with the file), a FileAccess (read, write, or both), and a FileShare (what other programs may do).
The simpler helpers OpenRead, OpenWrite, and Create are shortcuts with fixed settings. Always open streams in a using block so the file is closed.
This is a static method of System.IO.File. Call it as File.Open(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Pass a path and a FileMode. Add FileAccess and FileShare when you need them.
Opens for writing at the end. Append works only with write access.
File.Open returns the same FileStream that new FileStream(path, mode, access, share) would, with less typing.
Library declarations
C#
public static FileStream Open(string path, FileMode mode);
public static FileStream Open(string path, FileMode mode, FileAccess access);
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share);
Parameters
Parameter
Description
path
The file to open. Its folder must exist.
mode
What to do: CreateNew, Create, Open, OpenOrCreate, Truncate, or Append.
access
Optional. Read, Write, or ReadWrite. Defaults to ReadWrite, except for Append.
share
Optional. What other programs may do while the file is open: None, Read, Write, or ReadWrite.
Return Value
Return Value
Description
FileStream
A stream positioned at the start of the file, or at the end for Append. Dispose it when done.
C# Examples
Example 1 — Create a File, Then Open It for Reading
Use two different modes: one to write the file, one to read it back.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-open");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "abc.bin");
using (FileStream fs = File.Open(path, FileMode.Create, FileAccess.Write)) {
fs.WriteByte(65);
fs.WriteByte(66);
fs.WriteByte(67);
}
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read)) {
Console.WriteLine(fs.Length);
Console.WriteLine((char)fs.ReadByte());
}
Directory.Delete(root, true);
}
}
Output
3
A
How It Works
Create plus write.FileMode.Create makes the file (or empties an old one) and FileAccess.Write allows only writing. Byte values 65, 66, and 67 are the letters A, B, and C.
Open plus read. The second call uses FileMode.Open, which needs the file to exist, and FileAccess.Read, which blocks writing.
Read from the start. A newly opened stream sits at position zero. Length prints 3, and ReadByte returns 65, which the cast shows as A.
Example 2 — Add to the End with FileMode.Append
Open an existing file in append mode and write after the old content.
C#
using System;
using System.IO;
using System.Text;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-open-append");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "log.txt");
File.WriteAllText(path, "one");
using (FileStream fs = File.Open(path, FileMode.Append, FileAccess.Write)) {
byte[] more = Encoding.UTF8.GetBytes("-two");
fs.Write(more, 0, more.Length);
}
Console.WriteLine(File.ReadAllText(path));
Directory.Delete(root, true);
}
}
Output
one-two
How It Works
An existing start.WriteAllText saves one, so there is content to protect.
Append starts at the end. With FileMode.Append the stream position begins after the last byte, so the old content is kept.
Append wants write access. The call names FileAccess.Write on purpose, because ReadWrite with Append throws ArgumentException.
Result. The file now reads one-two.
Example 3 — CreateNew, Open, and OpenOrCreate Compared
Try three modes against a file that exists or is missing, and catch the expected errors.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-open-modes");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "mode.bin");
using (File.Open(path, FileMode.CreateNew)) { }
try {
File.Open(path, FileMode.CreateNew);
} catch (IOException) {
Console.WriteLine("CreateNew blocked");
}
File.Delete(path);
try {
File.Open(path, FileMode.Open);
} catch (FileNotFoundException) {
Console.WriteLine("Open needs a file");
}
using (FileStream fs = File.Open(path, FileMode.OpenOrCreate)) {
Console.WriteLine("OpenOrCreate length: " + fs.Length);
}
Directory.Delete(root, true);
}
}
Output
CreateNew blocked
Open needs a file
OpenOrCreate length: 0
How It Works
CreateNew once. The first CreateNew works because the file is new. The empty using block closes it at once.
CreateNew twice fails. The file now exists, so the second CreateNew throws IOException before any stream is returned. This is the safe way to avoid overwriting.
Open needs a file. After File.Delete, FileMode.Open throws FileNotFoundException, and the message shows the catch ran.
OpenOrCreate fixes it. The same call with OpenOrCreate creates an empty file, so its length is 0.
Frequently Asked Questions
A FileStream positioned at the start of the file, or at the end when you use FileMode.Append. Dispose it, normally with a using block.
Create makes the file or empties an existing one. CreateNew makes the file but throws IOException if it already exists, so it never overwrites.
OpenRead is a shortcut for File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read). Open lets you choose every setting.
It says what other programs may do while your stream is open. FileShare.None blocks everyone else, and FileShare.Read lets others read but not write.
Append works only with FileAccess.Write. Name FileAccess.Write explicitly, or leave the access out so the default is chosen for you.