如何打开bin文件 dwg.bin格式文件用什么软件打开


如何打开bin文件 dwg.bin格式文件用什么软件打开


BIN文件 , 即二进制文件,广泛应用于嵌入式 , 我们常用的Firmware通常会以BIN文件或者HEX文件格式存储,因此,对BIN文件的读写操作其实还是很普遍的,在这里,我记录一下我常用到的BIN文件操作 。
首先C# Winform中有Binary文件(BIN文件)的基本操作类 。如下所示
FileStream file_path = new FileStream(文件名, FileMode,FileAccess);//BinaryReader bin_read = new BinaryReader(file_path);BinaryWriter bin_write = new BinaryWriter(file_path);如上所示 , 如果是要读BIN文件,那么直接定义BinaryReader即可,如果是要写BIN文件,定义BInaryWriter 。读写的基本操作为:
读BIN文件的操作为:bin_read.ReadByte():返回值为读到的Byte值;bin_read.ReadBytes(count);返回值为个数为count的Byte数组 。还有很多不同返回格式,int,char等 , 我这里不一一赘述 。
写BIN文件的操作为:bin_write.Write(value):其中value就是要写的值,value可以是byte,int或者char等格式 。bin_write.Write(byte[] buffer, int index, int count);这个方法的含义就是将buffer数组中的一部分值(buffer数组的开始索引为index,长度为count),赋值至BIN文件当前位置 。
下面我举一个例子,BIN文件的写 , 从0写到255,256个byte 。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace TEST{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){SaveFileDialog save_file = new SaveFileDialog();save_file.Filter = "BIN文件|*.bin";if (save_file.ShowDialog() == DialogResult.OK){FileStream file_path = new FileStream(save_file.FileName, FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriter bin_write = new BinaryWriter(file_path);//创建BIN文件流byte[] init_byte = new byte[256];for (int temp = 0; temp < 256; temp++){init_byte[temp] = (byte)temp;}bin_write.Write(init_byte, 0, 256);//给BIN文件写内容bin_write.Flush();bin_write.Close();file_path.Close();}}}}文件运行结果为:
bin文件内容
那么写操作完成了,替换操作要怎么操作呢?实际中如果要实现HEX文件转换为BIN文件,那么替换功能将会非常有用 , 比如将其中的某几个数字改动一下,见代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace TEST{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){SaveFileDialog save_file = new SaveFileDialog();save_file.Filter = "BIN文件|*.bin";if (save_file.ShowDialog() == DialogResult.OK)//打开文件对话框{FileStream file_path = new FileStream(save_file.FileName, FileMode.OpenOrCreate,FileAccess.ReadWrite);BinaryWriter bin_write = new BinaryWriter(file_path);//创建BIN文件流byte[] init_byte = new byte[256];for (int temp = 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个字节bin_write.Write(0x08); //第255个字节改为08bin_write.Seek(8, SeekOrigin.Begin);//修改BIN文件当前位置至第8个字节bin_write.Write((byte)0x01);//第8个字节改为01bin_write.Write((byte)0x02);//第9个字节改为02bin_write.Write((byte)(0x90));//第10个字节改为90byte[] buffer = new byte[8];for (int temp = 0; temp < 8; temp++){buffer[temp] = (byte)(temp + 1);}bin_write.Seek(128, SeekOrigin.Begin);//修改BIN文件当前位置至第128个字节bin_write.Write(buffer, 2, 5);//将Buffer字节数组中的第2到到第7个数赋值到BIN文件的第128到133个字节bin_write.Write((byte)(0x90));//第134个字节改为08Console.WriteLine(file_path.Length);//看一下目前的文件大小file_path.SetLength(256);//文件大小已经超过256 , 只保留256个字节Console.WriteLine(file_path.Length);//看一下目前的文件大小bin_write.Flush();//释放文件资源bin_write.Close();file_path.Close();}}}}上述代码的运行结果为:

推荐阅读