Use BinaryReader to read file in binary format : BinaryReader « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;

class MainClass
{
  public static void Main() 
  {
    FileStream outStream = File.Create("c:\\BinaryTest.dat");

    BinaryWriter bw = new BinaryWriter(outStream);

    bw.Write( (int) 3);
    bw.Write( (decimal) 4.5);
    string s = "Test String";
    bw.Write(s);

    bw.Flush();
    bw.Close();

    FileStream inStream = File.OpenRead("c:\\BinaryTest.dat");

    BinaryReader br = new BinaryReader(inStream);

    int i = br.ReadInt32();
    decimal d = br.ReadDecimal();
    string s2 = br.ReadString();

    Console.WriteLine(i);
    Console.WriteLine(d);
    Console.WriteLine(s2);

    br.Close();
  }
}
3
4.5
Test String








15.26.BinaryReader
15.26.1.BinaryReader
15.26.2.Read decimal, strings and char from a binary file using the BinaryReader
15.26.3.Reset the file pointer to the start
15.26.4.Use BinaryReader to read file in binary format