BIN文件,即二進(jìn)制文件,廣泛應(yīng)用于嵌入式,我們常用的Firmware通常會(huì)以BIN文件或者HEX文件格式存儲(chǔ),因此,對(duì)BIN文件的讀寫(xiě)操作其實(shí)還是很普遍的,在這里,我記錄一下我常用到的BIN文件操作。
首先C#Winform中有Binary文件(BIN文件)的基本操作類。如下所示
FileStreamfile_path=newFileStream(文件名,FileMode,FileAccess);//BinaryReaderbin_read=newBinaryReader(file_path);BinaryWriterbin_write=newBinaryWriter(file_path);如上所示,如果是要讀BIN文件,那么直接定義BinaryReader即可,如果是要寫(xiě)B(tài)IN文件,定義BInaryWriter。讀寫(xiě)的基本操作為:
讀BIN文件的操作為:bin_read.ReadByte():返回值為讀到的Byte值;bin_read.ReadBytes(count);返回值為個(gè)數(shù)為count的Byte數(shù)組。還有很多不同返回格式,int,char等,我這里不一一贅述。
寫(xiě)B(tài)IN文件的操作為:bin_write.Write(value):其中value就是要寫(xiě)的值,value可以是byte,int或者char等格式。bin_write.Write(byte[]buffer,intindex,intcount);這個(gè)***的含義就是將buffer數(shù)組中的一部分值(buffer數(shù)組的開(kāi)始索引為index,長(zhǎng)度為count),賦值至BIN文件當(dāng)前位置。
下面我舉一個(gè)例子,BIN文件的寫(xiě),從0寫(xiě)到255,256個(gè)byte。
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespaceTEST{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){SaveFileDialogsave_file=newSaveFileDialog();save_file.Filter="BIN文件|*.bin";if(save_file.ShowDialog()==DialogResult.OK){FileStreamfile_path=newFileStream(save_file.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriterbin_write=newBinaryWriter(file_path);//創(chuàng)建BIN文件流byte[]init_byte=newbyte[256];for(inttemp=0;temp<256;temp++){init_byte[temp]=(byte)temp;}bin_write.Write(init_byte,0,256);//給BIN文件寫(xiě)內(nèi)容bin_write.Flush();bin_write.Close();file_path.Close();}}}}文件運(yùn)行結(jié)果為:
bin文件內(nèi)容
那么寫(xiě)操作完成了,替換操作要怎么操作呢?實(shí)際中如果要實(shí)現(xiàn)HEX文件轉(zhuǎn)換為BIN文件,那么替換功能將會(huì)非常有用,比如將其中的某幾個(gè)數(shù)字改動(dòng)一下,見(jiàn)代碼:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.IO;namespaceTEST{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){SaveFileDialogsave_file=newSaveFileDialog();save_file.Filter="BIN文件|*.bin";if(save_file.ShowDialog()==DialogResult.OK)//打開(kāi)文件對(duì)話框{FileStreamfile_path=newFileStream(save_file.FileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriterbin_write=newBinaryWriter(file_path);//創(chuàng)建BIN文件流byte[]init_byte=newbyte[256];for(inttemp=0;temp<256;temp++){init_byte[temp]=(byte)temp;}bin_write.Write(init_byte,0,256);//初始化BIN文件Console.WriteLine(file_path.Length);//看一下目前文件大小bin_write.Seek(255,SeekOrigin.Begin);//修改BIN文件當(dāng)前位置至第255個(gè)字節(jié)bin_write.Write(0x08);//第255個(gè)字節(jié)改為08bin_write.Seek(8,SeekOrigin.Begin);//修改BIN文件當(dāng)前位置至第8個(gè)字節(jié)bin_write.Write((byte)0x01);//第8個(gè)字節(jié)改為01bin_write.Write((byte)0x02);//第9個(gè)字節(jié)改為02bin_write.Write((byte)(0x90));//第10個(gè)字節(jié)改為90byte[]buffer=newbyte[8];for(inttemp=0;temp<8;temp++){buffer[temp]=(byte)(temp+1);}bin_write.Seek(128,SeekOrigin.Begin);//修改BIN文件當(dāng)前位置至第128個(gè)字節(jié)bin_write.Write(buffer,2,5);//將Buffer字節(jié)數(shù)組中的第2到到第7個(gè)數(shù)賦值到BIN文件的第128到133個(gè)字節(jié)bin_write.Write((byte)(0x90));//第134個(gè)字節(jié)改為08Console.WriteLine(file_path.Length);//看一下目前的文件大小file_path.SetLength(256);//文件大小已經(jīng)超過(guò)256,只保留256個(gè)字節(jié)Console.WriteLine(file_path.Length);//看一下目前的文件大小bin_write.Flush();//釋放文件資源bin_write.Close();file_path.Close();}}}}上述代碼的運(yùn)行結(jié)果為:
可以看到,BIN文件相應(yīng)的位置已經(jīng)更改完成,并且其他位置也沒(méi)有出現(xiàn)變動(dòng)。
這里我需要提一下,在做替換過(guò)程中,BIN文件的大小是會(huì)發(fā)生變化的,因此我用Console.WriteLine(file_path.Length)來(lái)監(jiān)控文件的大小變化??刂婆_(tái)輸出的結(jié)果為:
256,259,256
因此,我在代碼的最后將文件的長(zhǎng)度強(qiáng)行設(shè)置為256.這個(gè)不用擔(dān)心數(shù)據(jù),實(shí)際測(cè)試下來(lái),如果沒(méi)有file_path.SetLength(256)語(yǔ)句,那么結(jié)果如下:
可以看到后面幾個(gè)數(shù)據(jù)是無(wú)效的數(shù)據(jù),這個(gè)可以直接去掉。
以上是我平時(shí)比較常用的BIN文件操作。當(dāng)然,BIN文件的某一位的刪除和插入,我還沒(méi)有比較容易的辦法,不過(guò)BIN文件的刪除或者插入特定字符用的場(chǎng)景非常少,因此沒(méi)有過(guò)多的研究。希望以上內(nèi)容對(duì)大家有所幫助。