Example 1 — Save a Byte Array and Check the File
Write five bytes, then read them back.
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writeallbytesasync");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "data.bin");
byte[] data = { 1, 2, 3, 4, 5 };
await File.WriteAllBytesAsync(path, data);
byte[] back = await File.ReadAllBytesAsync(path);
Console.WriteLine(back.Length);
Console.WriteLine(string.Join(",", back));
Directory.Delete(root, true);
}
}
How It Works
- A small array.
data holds the numbers one to five. Each fits in a single byte. - One await saves it.
WriteAllBytesAsync creates data.bin and writes all five bytes. - Same bytes back. Reading the file returns five bytes, and
string.Join prints 1,2,3,4,5.
Example 2 — Write a Shorter Array to Replace the File
Write ten bytes, then three. The file ends up with three.
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writeallbytesasync-overwrite");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "block.bin");
await File.WriteAllBytesAsync(path, new byte[10]);
Console.WriteLine(new FileInfo(path).Length);
await File.WriteAllBytesAsync(path, new byte[] { 9, 9, 9 });
Console.WriteLine(new FileInfo(path).Length);
Directory.Delete(root, true);
}
}
How It Works
- Ten zero bytes.
new byte[10] makes an array of ten zeros, so the file is ten bytes long. - The second write replaces it. The file is emptied first, then three bytes are written.
- No leftovers. The length is now
3. The file does not keep the old seven extra bytes.
Example 3 — Turn Text into Bytes and Save It
Encode a word with an Encoding, save the bytes, and decode them again.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-writeallbytesasync-text");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "word.bin");
string word = "caf\u00e9";
byte[] bytes = Encoding.UTF8.GetBytes(word);
await File.WriteAllBytesAsync(path, bytes);
byte[] back = await File.ReadAllBytesAsync(path);
string text = Encoding.UTF8.GetString(back);
Console.WriteLine(back.Length);
Console.WriteLine(text.Length);
Console.WriteLine(text == word);
Directory.Delete(root, true);
}
}
How It Works
- Encode first. The word has four characters, but the letter with the accent takes two bytes in UTF-8, so
GetBytes returns five bytes. - Save and read. The file holds those five bytes, and reading returns the same count.
- Decode again.
GetString gives back four characters, and the comparison with the original word prints True.