private void btnReadBin_Click(object sender, EventArgs e) { /*Sets the file dialog box title.*/ openFileDialog1.Title = "Select *.bin"; /*Sets the current file name filter string.*/ openFileDialog1.Filter = "bin files (*.bin)|*.bin"; /*Runs a common dialog box with a default owner.*/ if (openFileDialog1.ShowDialog() == DialogResult.OK) { /*Show path*/ txtFilePath.Text = openFileDialog1.FileName; /*Open Bin file*/ FileStream myFile = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite); /*Initializes a new instance*/ BinaryReader myReader = new BinaryReader(myFile); /*Retrieve the length of Bin file*/ iLength = System.Convert.ToInt32(myFile.Length); /*Read to array*/ byte[] bData = new byte[51200]; bData = myReader.ReadBytes(iLength); myReader.Close(); myFile.Close(); } }
●Write Bin file
private void btnWrite_Click(object sender, EventArgs e) { /*Sets the file dialog box title.*/ saveFileDialog1.Title = "Select *.bin"; /*Sets the current file name filter string.*/ saveFileDialog1.Filter = "bin files (*.bin)|*.bin"; /*Runs a common dialog box with a default owner.*/ if (saveFileDialog1.ShowDialog() == DialogResult.OK) { /*Open Bin file*/ FileStream myFile = new FileStream(saveFileDialog1.FileName, FileMode.OpenOrCreate); /*Initializes a new instance*/ BinaryWriter myWriter = new BinaryWriter(myFile); /*Write to bin file*/ myWriter.Write(bData, 0, iLength); myWriter.Close(); myFile.Close(); } }
謝謝你的範例!對我幫助很大
回覆刪除我想幫忙點廣告,卻找不到連結!
謝謝!