一、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; } }
https://blog.csdn.net/qq_21381465/article/details/80016740
标签:bin,文件,fs,string,C#,res,FileStream,new From: https://www.cnblogs.com/Mars-0603/p/16731320.html