c# 將數據報文寫入到二進制文件中,如.dat


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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM