简体   繁体   English

我怎么知道C#中BinaryReader的当前偏移量?

[英]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. 我必须找出BinaryReader的当前偏移量。

You can obtain the underlying stream by 您可以通过获取基础流

var stream = reader.BaseStream;

and get the position by 并获得该职位

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM