首页 > 技术文章 > c# 将数据报文写入到二进制文件中,如.dat

mainmaster 2019-12-07 17:45 原文

1.文字模式

FileStream fs = new FileStream(string.Format("{0:D3}.cfg", currentcount), FileMode.Create);

//将文件名称格式化成***.cfg 格式

StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write(richTextBox1.Text);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();

2.二进制文件

byte[] bb = new byte[19968];
bb = string_yfc.HexStringToByteArray(richTextBox1.Text,1);
//将控件中的数据保存到数组中
System.Text.Encoding chs = System.Text.Encoding.GetEncoding("utf-8");
BinaryWriter fs = new BinaryWriter(new FileStream(string.Format("{0:D3}.dat", currentcount), FileMode.Create ));

fs.Write(bb);

fs.Close();

 

推荐阅读