2024年4月4日 星期四

C# 讀取Bin檔案並修改其內容


FileStream file_path = new FileStream(@BinFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryReader bin_read = new BinaryReader(file_path);

int dl = System.Convert.ToInt32(file_path.Length);
//讀取位元陣列
byte[] InData;
//讀取資料
InData = bin_read.ReadBytes(dl);
//釋放資源
bin_read.Close();
file_path.Close();

                 
FileStream temp_file_path = new FileStream(@TempBinFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter bin_write = new BinaryWriter(temp_file_path);//創建BIN文件流
bin_write.Write(InData);
bin_write.Seek(8, SeekOrigin.Begin);//修改BIN文件位置從第8字節

bin_write.Write((byte)0x01);//第8字節改為01
bin_write.Write((byte)0x02);//第9字節改為02
bin_write.Write((byte)(0x90));//第10字節改為90

bin_write.Close();
temp_file_path.Close();


沒有留言:

張貼留言