C# File OpenRead() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.OpenRead() opens an existing file for reading only and returns a FileStream. It is a short way to write File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read).

Use it when you want to read a file in pieces, such as a large binary file, instead of loading it all with ReadAllBytes. Other programs may still read the file while your stream is open, but they cannot write to it.

This is a static method of System.IO.File. Call it as File.OpenRead(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Call it in a using statement and read bytes with Read or ReadByte.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-openread.bin");
File.WriteAllBytes(path, new byte[] { 1, 2, 3 });

using (FileStream fs = File.OpenRead(path)) {
    Console.WriteLine(fs.ReadByte());
}
CallWhat it does
File.OpenRead(path)Opens an existing file for reading. Throws if the file is missing.
fs.Read(buffer, 0, count)Fills a byte array and returns how many bytes were really read. 0 means the end.
fs.LengthThe size of the file in bytes.

File.OpenRead gives you a byte stream. For text lines, File.OpenText returns a StreamReader, and File.ReadAllBytes loads everything at once.

Library declarations
C#
public static FileStream OpenRead(string path);

Parameters

ParameterDescription
pathThe file to read. It must exist.

Return Value

Return ValueDescription
FileStreamA read-only stream at position zero. Writing to it throws NotSupportedException.

C# Examples

Example 1 — Read a File into a Byte Array

Open a small file, read all its bytes, and turn them back into text.

C#
using System;
using System.IO;
using System.Text;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-openread");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "greeting.txt");
        File.WriteAllText(path, "Hello");

        using (FileStream fs = File.OpenRead(path)) {
            byte[] data = new byte[fs.Length];
            int count = fs.Read(data, 0, data.Length);

            Console.WriteLine(count);
            Console.WriteLine(Encoding.UTF8.GetString(data, 0, count));
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Size the buffer from the stream. fs.Length is the number of bytes in the file, so new byte[fs.Length] is exactly big enough for this small file.
  2. Use the returned count. Read returns how many bytes it placed in the array. Here that is 5.
  3. Bytes back to text. GetString(data, 0, count) decodes just those bytes into the string Hello.

Example 2 — The Stream Is Read-Only

Ask the stream what it can do, then try to write to it.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-openread-flags");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "ten.txt");
        File.WriteAllText(path, "0123456789");

        using (FileStream fs = File.OpenRead(path)) {
            Console.WriteLine("CanRead: " + fs.CanRead);
            Console.WriteLine("CanWrite: " + fs.CanWrite);
            Console.WriteLine("Length: " + fs.Length);

            try {
                fs.WriteByte(1);
            } catch (NotSupportedException) {
                Console.WriteLine("Cannot write");
            }
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Ask the stream. CanRead is True and CanWrite is False, because OpenRead asks the system for read access only.
  2. Ten characters, ten bytes. Each digit is one byte in UTF-8, so Length prints 10.
  3. Writing is refused. WriteByte throws NotSupportedException, so the file cannot be changed by mistake through this stream.

Example 3 — Read a File in Small Chunks

Use a loop and a small buffer, the pattern for files too big to load at once.

C#
using System;
using System.IO;
using System.Text;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-openread-chunks");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string path = Path.Combine(root, "letters.txt");
        File.WriteAllText(path, "ABCDEFGHIJ");

        using (FileStream fs = File.OpenRead(path)) {
            byte[] buffer = new byte[4];
            int count;
            while ((count = fs.Read(buffer, 0, buffer.Length)) > 0) {
                Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, count));
            }
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. A small buffer. The array holds four bytes, so each Read returns at most four. Real programs use sizes such as 4096 or more.
  2. Loop until zero. Read returns 0 at the end of the file, which stops the while loop. This is the standard way to know you are done.
  3. Decode only what arrived. The last pass returns two bytes, so GetString(buffer, 0, count) prints just IJ and ignores the old bytes still in the buffer.

Frequently Asked Questions

A read-only FileStream positioned at the start of an existing file. Writing to it throws NotSupportedException.
ReadAllBytes loads the whole file into one array and closes it. OpenRead gives you a stream so you can read in pieces, seek to a position, or stop early.
No. It throws FileNotFoundException. Use File.Open with FileMode.OpenOrCreate if you want the file created when it is missing.
Yes, as long as none of them opens it for writing in a way that blocks sharing. OpenRead allows other readers (FileShare.Read) but not writers.
Use OpenText (or StreamReader) for text, because it decodes the bytes for you. Use OpenRead for binary data or when you want to work with raw bytes.

Next: OpenText()

Open an existing text file and get a StreamReader to read it line by line.

OpenText() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful