NEED TO READ A DAT BINARY FILE
Loading
NEED TO READ A DAT BINARY FILE
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Nov 3, 2023, 7:42 AM EDT
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."); } } }Aymen AmriRank #1,022 · 1.9K reputationPosted Nov 3, 2023, 4:26 PM EDT
File.ReadAllBytes("yourFile.dat")
https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes?view=net-7.0
Vishal YelveRank #178 · 13.6K reputationPosted Nov 4, 2023, 7:50 AM EDT
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); }Jignesh KumarRank #91 · 22.9K reputationPosted Nov 5, 2023, 1:27 AM EDT
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); } } }