【C#】bin文件、任意类型文件操作汇总
一、bin文件
1、读写
/// <summary> /// 加载任意二进制文件 /// </summary> public static bool LoadBinaryFile(ref WaveData_Class testObj, string Fliepath) { bool res = false; try { FileStream load_read = File.Open(Fliepath, FileMode.Open, FileAccess.Read, FileShare.None); BinaryFormatter bf = new BinaryFormatter(); testObj = bf.Deserialize(load_read) as WaveData_Class; load_read.Close(); res = true; } catch (System.Exception ex) { res = false; MessageBox.Show(ex.ToString()); } return res; } /// <summary> /// 存储任意二进制文件文件 /// </summary> public static bool BinaryFileSave(WaveData_Class testObj, string Fliepath) { bool res = false; try { FileStream save_write; save_write = File.Open(Fliepath, System.IO.FileMode.OpenOrCreate); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(save_write, testObj); save_write.Close(); res = true; } catch (System.Exception ex) { res = false; MessageBox.Show(ex.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } return res; }
二、任意类型文件
1、读写
string sAllTVData = ""; //要写入文件里的内容变量 FileStream fs = new FileStream("D://tempdata.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); //创建文件对象 StreamReader reader = new StreamReader(fs, System.Text.Encoding.UTF8); //创建文件读取流对象 string s_TVinfo = ""; while ((s_TVinfo = reader.ReadLine()) != null) //实际工作中对写入文件的内容处理,我遇到的是把文件里的内容读出来,然后结合当前手里的数据进行分析处理,然后重新写入文件里,当然不需要这种需求,就只单纯打开文件,写入文件就行了 { //sAllTVData = sAllTVData + s_TVinfo +"\n"; //sTVDataSum++; } string data = "sn=" + _uScanCode + "&workOrderCode=" + sWorkOrderCode + "&lanMac=" + _sRecieveLanMac + "&wifiMac=" + _sRecieveWifiMac + "&btMac=" + _sRecieveBtMac + "&testUserName=" + sTestUser + "&testDateTime=" + sTimeStr; sAllTVData = data; StreamWriter writer = new StreamWriter(fs, System.Text.Encoding.UTF8); //创建写入文件流对象,指定文件对象和编码字符集 writer.Write(sAllTVData); //将内容写入文件,默认是覆盖 writer.Write(System.Environment.NewLine); //给写入文件内容后添加换行 //关闭所有打开的文件流对象 writer.Close(); reader.Close(); fs.Close();
或:
public void TestWrite() { string FileName = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".data"; FileStream DataFile = File.Open(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); int num = 1000; byte[] writebuff = new byte[num]; for (int h = 0; h < num; h++) { writebuff[h] = (byte)(h + 1); } DataFile.Seek(DataFile.Length, SeekOrigin.Begin); DataFile.Write(writebuff, 0, num); DataFile.Flush(); } public string TestLoad(string path) { int length = 0; string str = ""; try { FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite); length = (int)fs.Length; byte[] readbuf = new byte[length]; fs.Seek(0, SeekOrigin.Begin); fs.Read(readbuf, 0, length); for (int i = 0; i < length; i++) { str += readbuf[i].ToString() + " "; } fs.Close(); return str; } catch { return null; } }
/*******相与枕藉乎舟中,不知东方之既白*******/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!