4
Answers

how to read a binary file

NEED TO READ A DAT BINARY FILE 

Answers (4)

1
Photo of Jignesh Kumar
29 39.6k 2.9m 1y

Hello Evandro Eoveda,

Please refer code to read binery file in C#, You need to user "System.IO" name space to reade binery file in c#

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "Your binery file path"; 
        try
        {
            // Check if the file exists
            if (File.Exists(filePath))
            {
                // Open the file in binary mode and read its contents
                using (FileStream fileStream = File.OpenRead(filePath))
                {
                    byte[] buffer = new byte[fileStream.Length];

                    // Read the binary data into the buffer
                    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);

                    // Process the binary data (for example, display in hex format)
                    Console.WriteLine("Binary Data:");
                    foreach (byte data in buffer)
                    {
                        Console.Write($"{data:X2} "); // Display byte data in hexadecimal format
                    }
                }
            }
            else
            {
                Console.WriteLine("File does not exist.");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
        }
    }
}
1
Photo of Vishal Yelve
104 17.2k 637.4k 1y

Hi,

Try ths 

try
		{
			//creating an object of Stream
			FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
			//creating BinaryReader using Stream object
			using (BinaryReader reader = new BinaryReader(stream))
			{
				//reading data using Read() methods of different data types
				Console.WriteLine("String Value : " + reader.ReadString());
				Console.WriteLine("Double Value : " + reader.ReadDouble());
				Console.WriteLine("Boolean Value : " + reader.ReadBoolean());
			}
		}
		catch(Exception ex)
		{
			Console.WriteLine(ex.Message);
		}
1
Photo of Aymen Amri
561 1.9k 193.3k 1y

File.ReadAllBytes("yourFile.dat")

https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=net-7.0

1
Photo of Amit Mohanty
16 52.2k 6.1m 1y
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "your_file.dat"; // Replace with the path to your binary file.
        if (File.Exists(filePath))
        {
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        // Read an integer from the file.
                        int intValue = reader.ReadInt32();
                        Console.WriteLine($"Read integer value: {intValue}");

                        // Read a double from the file.
                        double doubleValue = reader.ReadDouble();
                        Console.WriteLine($"Read double value: {doubleValue}");

                        // Read a string from the file.
                        string stringValue = binaryReader.ReadString();
                        Console.WriteLine("Read string value: {stringValue}");

                        reader.Close();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error reading the file: {e.Message}");
            }
        }
        else
        {
            Console.WriteLine("The file does not exist.");
        }
    }
}