1
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
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
File.ReadAllBytes("yourFile.dat")
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=net-7.0
1
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.");
}
}
}