C# Convert File to Byte Array with Examples

  By : Suresh Dasari
  Posted On : 09-Apr-2023

Here, we will learn what is a byte array, how to convert a file to a byte array in c#, how to convert a byte array to a file in c#, and how to use ReadAllBytes and WriteAllBytes methods in c# with examples.

What is Byte Array?

In c#, the byte array is a data structure that is useful to store the sequence of bytes. A byte is a unit of information that consists of 8 bits, and it will represent a value between 0 and 255. The byte array will contain an array of objects, where each element in the array represents a single byte.

 

In c#, byte arrays are useful to store and perform operations on binary data such as image files, audio files, etc. To define byte array in c#, we need to use the byte keyword followed by byte array name and size.

 

Following is the example of defining the byte array in c#.

 

byte[] byteArray = new byte[200]

The above statement will create a byte array (byteArray) that can store 200 bytes.

C# Convert File to Byte Array

To perform any files related operations such as sending files from one system to another, creating or storing the files, etc. it’s a common task to convert a file to a byte array in c#.

 

In c#, by using File.ReadAllBytes() method we can convert any file to a byte array. The ReadAllBytes() method is available with the System.IO namespace.

 

Following is the example of converting the file to a byte array in c#.

 

using System.IO;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         string fpath = @"D:\images\test.png";
         byte[] byteArray = File.ReadAllBytes(fpath);
      }
   }
}

In this example, we are reading the contents of the file (test.png) into a byte array (byteArray) using File.ReadlAllBytes() method.

 

This is how we can convert file to byte array and perform any binary data-related operations in c#.

C# Convert Byte Array to File

In c#, by using File.WriteAllBytes() method, we can convert a byte array to a file and this method is available with System.IO namespace.

 

Following is the example of converting byte array to file in c#.

 

using System.IO;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         string sPath = @"D:\images\test.png";
         // Converting file content as a byte array
         byte[] byteArray = File.ReadAllBytes(sPath);

         //Converting Byte Array to File
         string dPath = @"D:\tutlane.png";
         File.WriteAllBytes(dPath, byteArray);
      }
   }
}

In this example, we are reading the file contents from one folder and converting it to a byte array using File.ReadAllBytes() method. After converting the file content to a byte array, we used File.WriteAllBytes() method to write the entire byte array to the newly specified file. If the file already exists, it will be overwritten.

 

If you are working with large files, instead of reading the entire file into memory at once it’s always advisable to read the file in chunks and process each chunk separately.

C# Convert File to Byte Array in Chunks

To process (read and write) files in chunks, we need to use FileStream class in c#. Following is the example of processing the files in chunks using FileStream class in c#.

 

using System.IO;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
         string sPath = @"C:\Learning\Articles\csharp-example.PNG";
         using (FileStream fStream = new FileStream(sPath, FileMode.Open))
         {
           byte[] byteArray = new byte[1024];
           int bytesRead;
           string dPath = @"C:\Learning\tutlane1.png";
           using (FileStream fileStream2 = new FileStream(dPath, FileMode.Create))
           {
              while ((bytesRead = fStream.Read(byteArray, 0, byteArray.Length)) > 0)
              {
                 fileStream2.Write(byteArray, 0, bytesRead);
              }
           }
         }
      }
   }
}

In this example, we used FileStream class to read the file from one folder and create the file in another folder. Here, we are reading the file in chunks of 1024 bytes and writing each chunk to the new file using the Write method. When we execute this example, the new file will be created in the specified folder path.

 

This is how we can use File.ReadAllBytes method to convert a file to byte array and File.WriteAllBytes method to write the byte array to another file. When working with large files, it's better to read the file in chunks using the FileStream class.