2011-04-05 194 views
0

如何修改此代碼以使用BinaryReader讀取二進制文件? ? 例Snort的日誌文件(文本和數字都包括)C#從讀取文本文件中讀取二進制文件

public string ReadFullFile() 
{ 
    using (StreamReader streamReader = new StreamReader(this.filename)) 
    { 
     return streamReader.ReadToEnd(); 
    } 
} 
+0

你想要什麼處理你閱讀的數據?你想要以某種方式處理它,或者你只是想要提供的代碼示例中的文件的全部內容? – Dyppl 2011-04-05 04:12:45

+0

我想分析數據包......所以我需要讀取日誌文件中的所有內容.. – 2011-04-05 04:59:28

回答

0

我不知道Snort的日誌,但二進制讀取部是這樣的:

class Record 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 
function ReadFullFile(Action<Record> processRecord) 
{ 
    using(var file = new FileStream("whatever.bin")) 
    { 
     using(var reader = new BinaryReader(file)) 
     { 
      processRecord(new Record 
      { 
       Id = reader.ReadInt32(), 
       Name = reader.ReadString(), 
      }); 
     } 
    } 
} 
0
public byte[] ReadFullFile() 
{ 
    return File.ReadAllBytes(this.FileName); 
}