我有C#代碼讀取文本文件並打印出來,看起來像這樣:如何找到沒有。字節的文本文件沒有讀取它?
StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));
byte[] buffer = new byte[100]; //is there a way to simply specify the length of this to be the number of bytes in the file?
sr.BaseStream.Read(buffer, 0, buffer.Length);
foreach (byte b in buffer)
{
label1.Text += b.ToString("x") + " ";
}
反正是有,我可以知道我的文件有多少字節了?
我想事先知道byte[] buffer
的長度,這樣在Read函數中,我可以簡單地傳入buffer.length
作爲第三個參數。
可能重複的[獲取Streamreader的長度](http://stackoverflow.com/questions/14637517/get-length-of-streamreader) – asawyer 2013-04-24 12:10:28
順便說一句,如果它的一個小文本文件讀取數據最簡單的方法就是'System.IO.File.ReadAllText(文件名)'http://msdn.microsoft.com/en-us/library/system.io.file.readalltext.aspx – asawyer 2013-04-24 12:24:05