创建txt文件并写如数据
1. 创建文件
public static void WriteDataFile(string message, string fileName) { FileStream fs = null; StreamWriter sw = null; string headStr = "测量点\t数据时间\t正向有功总\t正向有功尖\t正向有功峰\t正向有功平\t正向有功谷\t正向无功总\t正向无功尖\t正向无功峰\t正向无功平\t正向无功谷\t反向有功总\t反向无功总\t有功功率总\t有功功率A相\t有功功率B相\t有功功率C相\t无功功率总\t无功功率A相\t无功功率B相\t无功功率C相\t功率因数总\t功率因数A相\t功率因数B相\t功率因数C相\t电压A\t电压B\t电压C\t电流A\t电流B\t电流C"; bool isNew = false; string fileNameD = DateTime.Today.ToString("yyyyMMdd"); string bsFileName = Application.StartupPath + ("/Data/" + fileNameD + "/" + fileName + "Data.txt"); if (File.Exists(bsFileName) == false) { fs = new FileStream(bsFileName, FileMode.Create); fs.Close(); isNew = true; } try { lock (bsFileName) { fs = new FileStream(bsFileName, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); if (message == "") { if (isNew == true) { sw.WriteLine(headStr); } } else { sw.WriteLine(message); } } } catch (Exception ee) { Console.Write(ee.ToString()); Log log = Log.Instance(); log.Error("WriteFile" + ee.ToString()); } finally { if (fs != null) { sw.Close(); } if (fs != null) { fs.Close(); } } }
2. 写入数据或穿件文件
Common.WriteDemandFile(Protocol.AnalyseDataKeDong(message), dataFileName);