c#读取二进制文件 文件结束标志
FileStream fs = File.OpenRead(path); BinaryReader brReader = new BinaryReader(fs); while (brReader.BaseStream.Position < brReader.BaseStream.Length) { //TODO } brReader.Close();
using (BinaryReader br = new BinaryReader(fs)) { while (br.PeekChar() > -1) { //TODO } }
以上两种方法可以判断文件结束
下边是简单记录日志时候用的写入到txt文件里的方法
string logpath = Settings.LogPath;//d:\logs string logFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; string logFilePath = logpath + "\\" + logFileName; string log = DateTime.Now.ToString() + ": " + e.Message; if (!File.Exists(logFilePath)) File.Create(logFilePath).Close(); using (StreamWriter sw = File.AppendText(logFilePath)) { sw.WriteLine("{0}", log); sw.Flush(); sw.Close(); }