日期:2014-05-18  浏览次数:21020 次

c#如何读取二进制文件?
文件内容如下(这个是用旧的vb程序读出来写到一个文本里面的):
431 
 2248 
 10818 
 74 
 76 
 1951 
 1 
 32744 
 32744 
 32744 
 ……
 ……
然后我用c#读取:
FileStream myFS = new FileStream(myFname, FileMode.Open); //myname是二进制文件
StreamWriter txtTmp = new StreamWriter("test.txt");
BinaryReader br = new BinaryReader(myFS);
byte[] tmp = new byte[myFS.Length];
br.Read(tmp, 0, (int)myFS.Length);
txtTmp.Write(tmp[5]);
myFS.Close();

不成功,tmp数组的每个元素都是0,写入的文本文件也是0,怎么回事呢?

------解决方案--------------------
参考
------解决方案--------------------
FileStream 读出来,Length属性就是字节数
------解决方案--------------------
byte[] tmp = System.IO.File.ReadAllBytes(myFname)
------解决方案--------------------
File.ReadAllBytes(Path);
File.ReadAllLines(Path);
File.ReadAllText(Path);
可以尝试这几种方法~

------解决方案--------------------
StreamWriter 会把字符编码成字节。可能会有问题,建议直接用stream读写。