Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know current offset of BinaryReader in C#?

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.

like image 230
Joshua Son Avatar asked Feb 23 '14 13:02

Joshua Son


2 Answers

You can obtain the underlying stream by

var stream = reader.BaseStream;

and get the position by

stream.Position
like image 109
Samuel Avatar answered Oct 26 '22 23:10

Samuel


BinaryReader br=null;
/ * init, read, ...*/
long pos=br.BaseStream.Position;
like image 45
huseyin tugrul buyukisik Avatar answered Oct 27 '22 00:10

huseyin tugrul buyukisik