二進位檔案存取 二進位檔案存取
  .NET       ez      2011-11-29

.NET Framework提供了BinaryReader與BinaryWriter用來處理二進位資料

讀取

FileStream myFile = File.Open(@"C:\a.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryReader myReader = new BinaryReader(myFile);
int dl = Convert.ToInt32(myFile.Length);
byte[] myData = myReader.ReadBytes(dl);
myReader.Close();
myFile.Close();

寫入

FileStream myFile = File.Open(@"C:\a.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter myWriter = new BinaryWriter(myFile);
byte[] myByte = new byte[] { 1, 2, 3, 4, 5, 6 };
myWriter.Write(myByte);
myWriter.Close();
myFile.Close();

 


標籤:   .NET

我要留言