2011-04-05 273 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); 
}