Home C# File Methods WriteAllBytes() C# File WriteAllBytes() Method 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.
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); Call What 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 public static void WriteAllBytes(string path, byte[] bytes); Parameters Parameter Description 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 Value Description voidNothing is returned. The file length equals bytes.Length.
Important
A null array throws. ArgumentNullException is thrown before the file is touched. An existing file is replaced without warning, a missing folder throws DirectoryNotFoundException, and a read-only file throws UnauthorizedAccessException.
C# Examples 1. Write five bytes 2. Text as bytes 3. Round trip and null Example 1 — Write a Small Byte Array Save five bytes and check the size and the first byte.
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 Plain numbers. The array holds five values between 0 and 255. Each one becomes exactly one byte in the file.Size equals element count. The array read back has 5 elements, the same as the one that was written.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.
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 You do the encoding. Encoding.UTF8.GetBytes("Hello") returns five bytes. WriteAllBytes itself knows nothing about text.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.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.
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);
}
} 256
True
Null array rejected How It Works 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.Nothing is lost. The file is not changed by encoding rules, so all values return and SequenceEqual prints True.Null is refused. Passing null throws ArgumentNullException. Check for null yourself when the array comes from outside.Frequently Asked Questions What does File.WriteAllBytes do? 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.
What is the difference between WriteAllBytes and WriteAllText? 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.
Does File.WriteAllBytes overwrite an existing file? Yes, the old content is replaced. To add bytes to the end, open the file with FileMode.Append and write to the stream.
Why does WriteAllBytes throw ArgumentNullException? The bytes argument was null. Pass an empty array (new byte[0]) if you want an empty file.
Can I write a large file with File.WriteAllBytes? Only if the whole array fits in memory. For very large data, write chunks to a FileStream from File.Create or File.OpenWrite.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion