If you have bin file stored in your PC and you want to read them then BinaryReader may help you lot. BinaryReader class is used to reading binary files. A binary file stored data in different layout that is more efficient for machine but not convenient for human. BinaryReader makes this job simpler and show you exact data stored in bin file.

Notes:

  1. BinaryReader handles Binary (.bin) files.
  2. It reads primitive data types as binary values in a specific encoding.
  3. BinaryReader class provides methods that simplify reading primitive data types from stream.

For reading binary file, you need to create binary file first using BinaryWriter Class.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace BinaryReader_Class
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteBinaryFile();
            ReadBinaryFile();
            Console.ReadKey();
        }
        static void WriteBinaryFile()
        {
            using (BinaryWriter writer = new BinaryWriter(File.Open("D:\\binaryfile.bin", FileMode.Create)))
            {
                //Writting Error Log
                writer.Write("0x80234400");
                writer.Write("Windows Explorer Has Stopped Working");
                writer.Write(true);
            }
        }
        static void ReadBinaryFile()
        {
            using (BinaryReader reader = new BinaryReader(File.Open("D:\\binaryfile.bin", FileMode.Open)))
            {
                Console.WriteLine("Error Code : " + reader.ReadString());
                Console.WriteLine("Message : " + reader.ReadString());
                Console.WriteLine("Restart Explorer : " + reader.ReadBoolean());
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *