2014-02-23 40 views
6

我有以下來源:如何知道C#中BinaryReader的當前偏移量?

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); 
    } 
} 

我必須找出當前BinaryReader在偏移。

+1

得到的位置不要注意,您做出強烈的假設,即BinaryReader在不從基礎緩衝區的字節流。它會緩衝字節。但不是以一種會以字節爲單位的方式。 –

回答

7

您可以通過

var stream = reader.BaseStream; 

獲取基礎數據流,並通過

stream.Position 
+0

非常感謝您的回答。 –

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

謝謝你的回答。 –

相關問題