C# FileAccess
last modified November 6, 2026
In this article we show how to use the FileAccess enumeration in
C#.
FileAccess from the System.IO namespace specifies
what a program wants to do with an opened file: read it, write it, or both. It
is passed to the FileStream constructor, File.Open,
and FileInfo.Open, together with a FileMode that
says how to open the file and an optional FileShare that says
what other programs may do with it at the same time.
Requesting only the access we need is good practice. A file opened for reading can be read-only on disk, and other programs may be allowed to read it while we have it open.
The examples create a demo directory in the working directory and
delete it at the end.
The enumeration values
FileAccess has three values.
foreach (FileAccess access in Enum.GetValues<FileAccess>())
{
Console.WriteLine($"{access,-10} {(int)access}");
}
FileAccess both = FileAccess.Read | FileAccess.Write;
Console.WriteLine($"Read | Write = {both}");
Console.WriteLine($"ReadWrite has Write: {FileAccess.ReadWrite.HasFlag(FileAccess.Write)}");
We print the names and numbers of the values.
FileAccess both = FileAccess.Read | FileAccess.Write;
The enumeration has the Flags attribute. ReadWrite
is the combination of Read and Write.
$ dotnet run Read 1 Write 2 ReadWrite 3 Read | Write = ReadWrite ReadWrite has Write: True
Read access
A stream opened with FileAccess.Read can only be read.
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "message.txt");
File.WriteAllText(path, "an old falcon");
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Console.WriteLine($"CanRead: {fs.CanRead}, CanWrite: {fs.CanWrite}");
using var reader = new StreamReader(fs, leaveOpen: true);
Console.WriteLine(reader.ReadToEnd());
try
{
fs.WriteByte(65);
}
catch (NotSupportedException e)
{
Console.WriteLine($"NotSupportedException: {e.Message}");
}
}
Directory.Delete("demo", true);
We read the content of a file and then try to write a byte.
Console.WriteLine($"CanRead: {fs.CanRead}, CanWrite: {fs.CanWrite}");
The CanRead and CanWrite properties of the stream
reflect the requested access.
using var reader = new StreamReader(fs, leaveOpen: true);
With leaveOpen, disposing the reader does not close the stream,
so we can keep using it.
fs.WriteByte(65);
Writing to a read-only stream throws NotSupportedException. This
is a programming error, not an I/O error, so it does not derive from
IOException.
$ dotnet run CanRead: True, CanWrite: False an old falcon NotSupportedException: Stream does not support writing.
Write access
FileAccess.Write is used for logs and output files that the
program does not read.
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "app.log");
for (int i = 1; i <= 3; i++)
{
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write);
using var writer = new StreamWriter(fs);
writer.WriteLine($"entry {i}");
}
Console.Write(File.ReadAllText(path));
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Write))
{
Console.WriteLine($"CanRead: {fs.CanRead}, CanWrite: {fs.CanWrite}");
try
{
fs.ReadByte();
}
catch (NotSupportedException e)
{
Console.WriteLine($"NotSupportedException: {e.Message}");
}
}
Directory.Delete("demo", true);
We append three entries to a log file, opening it each time.
using var fs = new FileStream(path, FileMode.Append, FileAccess.Write);
FileMode.Append opens the file, or creates it, and moves to the
end. It works only with FileAccess.Write.
fs.ReadByte();
Reading from a write-only stream throws NotSupportedException.
$ dotnet run entry 1 entry 2 entry 3 CanRead: False, CanWrite: True NotSupportedException: Stream does not support reading.
Read and write access
FileAccess.ReadWrite allows updating a file in place.
using System.Text;
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "counter.txt");
File.WriteAllText(path, "41");
using (var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
{
byte[] buffer = new byte[fs.Length];
fs.ReadExactly(buffer);
int count = int.Parse(Encoding.UTF8.GetString(buffer)) + 1;
byte[] data = Encoding.UTF8.GetBytes(count.ToString());
fs.Position = 0;
fs.Write(data);
fs.SetLength(data.Length);
}
Console.WriteLine(File.ReadAllText(path));
Directory.Delete("demo", true);
We read a counter from a file, increment it, and write it back through the same stream.
fs.ReadExactly(buffer);
ReadExactly reads the whole file into the buffer.
fs.Position = 0; fs.Write(data); fs.SetLength(data.Length);
We move back to the start and overwrite the content. SetLength
cuts off the rest of the file, which matters when the new content is shorter
than the old one.
$ dotnet run 42
Invalid combinations
Some file modes modify the file and therefore require write access.
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "data.txt");
File.WriteAllText(path, "data");
Try(FileMode.Append, FileAccess.Read);
Try(FileMode.Append, FileAccess.ReadWrite);
Try(FileMode.Truncate, FileAccess.Read);
Try(FileMode.Create, FileAccess.Read);
Try(FileMode.Open, FileAccess.Read);
Try(FileMode.Truncate, FileAccess.Write);
Directory.Delete("demo", true);
void Try(FileMode mode, FileAccess access)
{
try
{
using var fs = new FileStream(path, mode, access);
Console.WriteLine($"{mode} + {access}: ok");
}
catch (ArgumentException)
{
Console.WriteLine($"{mode} + {access}: ArgumentException");
}
}
We open a file with several combinations of mode and access.
catch (ArgumentException)
Append requires Write alone, and
Truncate, Create, and CreateNew do not
work with Read. The constructor checks the arguments and throws an
ArgumentException before touching the file.
$ dotnet run Append + Read: ArgumentException Append + ReadWrite: ArgumentException Truncate + Read: ArgumentException Create + Read: ArgumentException Open + Read: ok Truncate + Write: ok
Default access
When no access is given, it depends on the method and the mode.
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "data.txt");
File.WriteAllText(path, "data");
Show("new FileStream(path, Open)", new FileStream(path, FileMode.Open));
Show("new FileStream(path, Append)", new FileStream(path, FileMode.Append));
Show("File.OpenRead", File.OpenRead(path));
Show("File.OpenWrite", File.OpenWrite(path));
Show("File.Open(path, Open)", File.Open(path, FileMode.Open));
Show("File.Create", File.Create(path));
Directory.Delete("demo", true);
static void Show(string label, FileStream fs)
{
using (fs)
{
Console.WriteLine($"{label,-30} read: {fs.CanRead,-5} write: {fs.CanWrite}");
}
}
We open a file in several ways and print the access of the stream.
Show("new FileStream(path, Append)", new FileStream(path, FileMode.Append));
The FileStream(string, FileMode) constructor uses
ReadWrite, except for Append, where it uses
Write.
Show("File.OpenRead", File.OpenRead(path));
Show("File.OpenWrite", File.OpenWrite(path));
File.OpenRead and File.OpenWrite request only the
access their names say. Prefer File.OpenRead for reading: a
stream opened with the default ReadWrite fails on read-only
files.
$ dotnet run new FileStream(path, Open) read: True write: True new FileStream(path, Append) read: False write: True File.OpenRead read: True write: False File.OpenWrite read: False write: True File.Open(path, Open) read: True write: True File.Create read: True write: True
Access and file permissions
The operating system checks the requested access against the permissions of the file.
Directory.CreateDirectory("demo");
string path = Path.Combine("demo", "protected.txt");
File.WriteAllText(path, "do not change");
File.SetAttributes(path, FileAttributes.ReadOnly);
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Console.WriteLine($"opened for reading, {fs.Length} bytes");
}
try
{
using var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
}
catch (UnauthorizedAccessException)
{
Console.WriteLine("ReadWrite on a read-only file: UnauthorizedAccessException");
}
File.SetAttributes(path, FileAttributes.Normal);
Directory.Delete("demo", true);
We make a file read-only and open it twice.
using var fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
Asking for write access to a read-only file throws an
UnauthorizedAccessException, even if the program never writes.
Read access succeeds. On Unix the root user can write to read-only files, so
the exception is not thrown when the program runs as root.
File.SetAttributes(path, FileAttributes.Normal);
On Windows, deleting a read-only file fails, so we clear the attribute before deleting the directory.
$ dotnet run opened for reading, 13 bytes ReadWrite on a read-only file: UnauthorizedAccessException
Source
In this article we have worked with the FileAccess enumeration in
C#.
Author
List all C# tutorials.