C# File ReadAllBytes() Method

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

Definition

In C#, File.ReadAllBytes() reads a whole file and returns its raw content as a byte[]. It does no decoding at all, so it works for images, zip files, and any other binary data as well as text.

The array has exactly as many elements as the file has bytes. Like the other ReadAll methods it opens the file, reads it, and closes it for you.

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

Syntax

Pass the path and store the bytes in an array.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-readallbytes.bin");
File.WriteAllBytes(path, new byte[] { 72, 105, 33 });

byte[] bytes = File.ReadAllBytes(path);

Console.WriteLine(bytes.Length);
CallWhat it does
File.ReadAllBytes(path)Returns every byte of the file as a byte[].
Encoding.UTF8.GetString(bytes)Turns the bytes into text, when you know the file holds UTF-8.
BitConverter.ToString(bytes)Shows the bytes as hex, such as 48-69. Good for looking inside a file.

The array must fit in memory, and a single array tops out at about 2 GB. For huge files, read in chunks with File.OpenRead and Read.

Library declarations
C#
public static byte[] ReadAllBytes(string path);

Parameters

ParameterDescription
pathThe file to read. It must exist.

Return Value

Return ValueDescription
byte[]The complete content of the file. An empty file gives an array with length 0.

C# Examples

Example 1 — Read Bytes and Turn Them into Text

Write three known bytes, read them back, and decode them as ASCII.

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

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

        string path = Path.Combine(root, "hi.bin");
        File.WriteAllBytes(path, new byte[] { 72, 105, 33 });

        byte[] bytes = File.ReadAllBytes(path);
        Console.WriteLine(bytes.Length);
        Console.WriteLine(bytes[0]);
        Console.WriteLine(Encoding.ASCII.GetString(bytes));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Known bytes. The numbers 72, 105, and 33 are the ASCII codes of H, i, and !.
  2. An element per byte. The array has 3 elements, and bytes[0] prints the number 72, not a letter.
  3. You pick the decoding. ReadAllBytes knows nothing about text. Encoding.ASCII.GetString turns the numbers into Hi!.

Example 2 — Look at a Byte Order Mark in Hex

Save a letter with a UTF-8 BOM and print the bytes as hex.

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

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

        string plain = Path.Combine(root, "plain.txt");
        string withBom = Path.Combine(root, "bom.txt");

        File.WriteAllText(plain, "A");
        File.WriteAllText(withBom, "A", new UTF8Encoding(true));

        Console.WriteLine(BitConverter.ToString(File.ReadAllBytes(plain)));
        Console.WriteLine(BitConverter.ToString(File.ReadAllBytes(withBom)));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two files, one letter. Both files hold the letter A, but they were saved with different encoding objects.
  2. The default has no BOM. WriteAllText writes UTF-8 without a BOM, so the plain file is the single byte 41, which is the hex code of A.
  3. The BOM is three bytes. new UTF8Encoding(true) adds EF BB BF before the text. ReadAllText hides those bytes from you, but ReadAllBytes shows them.

Example 3 — Compare Two Files Byte by Byte

Check whether two files hold exactly the same content.

C#
using System;
using System.IO;
using System.Linq;

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

        string a = Path.Combine(root, "a.bin");
        string b = Path.Combine(root, "b.bin");

        File.WriteAllBytes(a, new byte[] { 1, 2, 3, 4 });
        File.Copy(a, b);
        Console.WriteLine(File.ReadAllBytes(a).SequenceEqual(File.ReadAllBytes(b)));

        File.WriteAllBytes(b, new byte[] { 1, 2, 3, 9 });
        Console.WriteLine(File.ReadAllBytes(a).SequenceEqual(File.ReadAllBytes(b)));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A copy is identical. File.Copy makes b.bin from a.bin, so every byte matches.
  2. Compare contents, not arrays. Using == on two arrays checks whether they are the same object, so it would give False. SequenceEqual from LINQ checks each element and prints True.
  3. One byte changes the answer. After the last byte of b.bin is changed to 9, the same test prints False. For big files, compare in chunks or compare hashes instead.

Frequently Asked Questions

A byte array holding the complete content of the file. An empty file gives an array of length zero.
Yes, but you get raw bytes, not characters. Use Encoding.UTF8.GetString(bytes) to turn them into a string, or call File.ReadAllText directly.
No. It loads the whole file into memory, and arrays are limited to about 2 GB. Use File.OpenRead and read in chunks for big files.
Compare their lengths first, then compare the bytes with SequenceEqual, or compute a hash of each file. Do not use == on the byte arrays.
ReadAllBytes returns the entire content at once. OpenRead gives a FileStream, so you can read part of the file, move to a position, or stop early.

Next: ReadAllLines()

Read every line of a text file into a string array.

ReadAllLines() 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.

11 people found this page helpful