C# File WriteAllBytes() Method

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

Definition

In C#, File.WriteAllBytes() saves a byte[] into a file. It creates the file or replaces the old one, then closes the file. Nothing is encoded or changed: the file holds exactly the bytes you give.

Use it for binary data such as images, downloads, or serialized objects. It pairs naturally with File.ReadAllBytes.

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

Syntax

Pass the path and the byte array.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-writeallbytes.bin");
byte[] data = { 1, 2, 3, 4, 5 };

File.WriteAllBytes(path, data);

Console.WriteLine(new FileInfo(path).Length);
CallWhat it does
File.WriteAllBytes(path, bytes)Creates or overwrites the file so it holds exactly bytes.
File.WriteAllBytes(path, Encoding.UTF8.GetBytes(text))Saves text with an encoding you pick yourself.
File.WriteAllBytes(path, new byte[0])Creates an empty file.

WriteAllBytes writes the array in one go. For a big stream of data, write in chunks to a FileStream from File.Create instead.

Library declarations
C#
public static void WriteAllBytes(string path, byte[] bytes);

Parameters

ParameterDescription
pathThe file to write. It is created if missing, and its folder must exist.
bytesThe data to save. It must not be null.

Return Value

Return ValueDescription
voidNothing is returned. The file length equals bytes.Length.

C# Examples

Example 1 — Write a Small Byte Array

Save five bytes and check the size and the first byte.

C#
using System;
using System.IO;

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

        string path = Path.Combine(root, "numbers.bin");
        byte[] data = { 10, 20, 30, 40, 50 };

        File.WriteAllBytes(path, data);

        byte[] back = File.ReadAllBytes(path);
        Console.WriteLine(back.Length);
        Console.WriteLine(back[0]);
        Console.WriteLine(back[4]);

        Directory.Delete(root, true);
    }
}

How It Works

  1. Plain numbers. The array holds five values between 0 and 255. Each one becomes exactly one byte in the file.
  2. Size equals element count. The array read back has 5 elements, the same as the one that was written.
  3. Order is kept. The first byte is 10 and the last (index 4) is 50.

Example 2 — Save Text by Converting It to Bytes First

Encode a string yourself, save the bytes, and read the file as text.

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

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

        string path = Path.Combine(root, "greeting.txt");
        byte[] bytes = Encoding.UTF8.GetBytes("Hello");

        File.WriteAllBytes(path, bytes);

        Console.WriteLine(bytes.Length);
        Console.WriteLine(File.ReadAllText(path));

        Directory.Delete(root, true);
    }
}

How It Works

  1. You do the encoding. Encoding.UTF8.GetBytes("Hello") returns five bytes. WriteAllBytes itself knows nothing about text.
  2. Same file as WriteAllText. Because the bytes are plain UTF-8, ReadAllText reads them as Hello. A file is just bytes; text is one way to read them.
  3. Why do it this way. It is useful when you need an encoding or a BOM that you control exactly, or when the bytes arrive from a network call.

Example 3 — Round Trip All 256 Byte Values

Write every possible byte, read them back, and test the null check.

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

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

        string path = Path.Combine(root, "all.bin");
        byte[] all = new byte[256];
        for (int i = 0; i < all.Length; i++) {
            all[i] = (byte)i;
        }

        File.WriteAllBytes(path, all);
        byte[] back = File.ReadAllBytes(path);

        Console.WriteLine(back.Length);
        Console.WriteLine(back.SequenceEqual(all));

        try {
            File.WriteAllBytes(path, null);
        } catch (ArgumentNullException) {
            Console.WriteLine("Null array rejected");
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Every byte value once. The loop fills the array with 0 to 255. Casting i to byte is safe because the loop stops at 255.
  2. Nothing is lost. The file is not changed by encoding rules, so all values return and SequenceEqual prints True.
  3. Null is refused. Passing null throws ArgumentNullException. Check for null yourself when the array comes from outside.

Frequently Asked Questions

It saves a byte array to a file, creating the file or replacing an existing one. The file ends up with exactly the bytes you pass.
WriteAllText turns a string into bytes with an encoding. WriteAllBytes writes the bytes as they are, so you control the encoding or the binary format.
Yes, the old content is replaced. To add bytes to the end, open the file with FileMode.Append and write to the stream.
The bytes argument was null. Pass an empty array (new byte[0]) if you want an empty file.
Only if the whole array fits in memory. For very large data, write chunks to a FileStream from File.Create or File.OpenWrite.

Next: WriteAllLines()

Write an array or sequence of strings as lines, replacing the file.

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

14 people found this page helpful