8

I have source below:

public static void DisplayValues()
{
    float aspectRatio;
    string tempDirectory;
    int autoSaveTime;
    bool showStatusBar;

    if (File.Exists(fileName))
    {
        using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
        {
            aspectRatio = reader.ReadSingle();
            tempDirectory = reader.ReadString();
    ------------------------------------------------> I want to know current offset.
            autoSaveTime = reader.ReadInt32();
            showStatusBar = reader.ReadBoolean();
        }

        Console.WriteLine("Aspect ratio set to: " + aspectRatio);
        Console.WriteLine("Temp directory is: " + tempDirectory);
        Console.WriteLine("Auto save time set to: " + autoSaveTime);
        Console.WriteLine("Show status bar: " + showStatusBar);
    }
}

I have to find out current offset of BinaryReader.

Joshua Son
  • 1,839
  • 6
  • 31
  • 51
  • 2
    Do note that you make the strong assumption that BinaryReader doesn't buffer bytes from the base stream. It does buffer bytes. But not in a way that will byte you. – Hans Passant Feb 23 '14 at 14:24

2 Answers2

12

You can obtain the underlying stream by

var stream = reader.BaseStream;

and get the position by

stream.Position
Samuel
  • 6,126
  • 35
  • 70
1
BinaryReader br=null;
/ * init, read, ...*/
long pos=br.BaseStream.Position;
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97