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

不想你离开。 提交于 2021-02-08 13:32:34

问题


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.


回答1:


You can obtain the underlying stream by

var stream = reader.BaseStream;

and get the position by

stream.Position



回答2:


BinaryReader br=null;
/ * init, read, ...*/
long pos=br.BaseStream.Position;


来源:https://stackoverflow.com/questions/21969045/how-do-i-know-current-offset-of-binaryreader-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!