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