//write csv log public static void WriteCSVDate(string WriteString, string fileName, string path, string title) { bool bfirst = false; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } string strFile = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + fileName + ".csv"; if (File.Exists(strFile) == false) { bfirst = true; } try { StreamWriter swf = new StreamWriter(strFile, true, Encoding.Default); if (bfirst) { swf.WriteLine(title); } swf.WriteLine(WriteString); swf.Close(); } catch (System.Exception ex) { HintMessageInfo.MessageTopMost("csv文件操作出错:" + ex.Message); } }
/// <summary> /// 读取指定路径的CSV文件中的内容 /// </summary> /// <param name="filename"></param> /// <returns></returns> public static List <string> GetCsvInfon(string filename) { List <string> mydata = new List <string>(); try { StreamReader read = new StreamReader(filename, Encoding.Default); string strcontent = read.ReadToEnd(); read.Close(); string[] data = System.Text.RegularExpressions.Regex.Split(strcontent, "\r\n"); for (int j = 1; j < data.Count(); j++) { if (data[j] != "") { mydata.Add(data[j]); } } } catch (Exception ex) { HintMessageInfo.MessageTopMost("csv文件操作出错:" + ex.Message); } return(mydata); }
/// <summary> /// 读取指定目录下所有保存时间在指定时间以内的文件,并显示在表格 /// </summary> /// <param name="path"></param> 指定目录 /// <param name="savetime"></param> 指定时间(天数) /// <param name="datagrid"></param>指定表格 public static void ReadCsvDateToDataGridView(string path, double savetime, DataGridView datagrid) { List <string> FileNames = new List <string>(); FileNames = FindFile(path, savetime, FileNames); datagrid.Rows.Clear(); try { for (int i = 0; i < FileNames.Count(); i++) { StreamReader read = new StreamReader(FileNames[i], Encoding.Default); string strcontent = read.ReadToEnd(); read.Close(); string[] data = System.Text.RegularExpressions.Regex.Split(strcontent, "\r\n"); for (int j = 1; j < data.Count(); j++) { if (data[j] != "") { string[] mydata = data[j].Split(','); bool HasData = false; for (int h = 0; h < mydata.Count(); h++) { if (mydata[h] != "") { HasData = true; } } if (HasData) { datagrid.Rows.Add(mydata); } } } } } catch (System.Exception ex) { HintMessageInfo.MessageTopMost("csv文件操作出错:" + ex.Message); } }
/// <summary> /// 查找目录下所有在指定时间段(天数)以内的文件 /// </summary> /// <param name="path"></param> 目录 /// <param name="startime"></param> 起始时间 /// <param name="endtime"></param> 结束时间 /// <param name="filename"></param> /// <returns></returns> public static List <string> FindFile(string path, DateTime startime, DateTime endtime, List <string> filename) { if ((startime - endtime).TotalDays > 0) { HintMessageInfo.MessageTopMost("查询时间设置错误"); return(filename); } string[] DirectoryNames = Directory.GetDirectories(path); string[] FileNames = Directory.GetFiles(path); for (int i = 0; i < FileNames.Count(); i++) { if ((File.GetCreationTime(FileNames[i]) - startime).TotalDays >= 0 && (endtime - File.GetCreationTime(FileNames[i])).TotalDays >= 0) { filename.Add(FileNames[i]); } } for (int j = 0; j < DirectoryNames.Count(); j++) { FindFile(DirectoryNames[j], startime, endtime, filename); //递归调用 } return(filename); }
/// <summary> /// 从指定的CSV文件中获取指定信息的数组 /// </summary> /// <param name="filename"></param> 指定的CSV文件名 /// <param name="mesg"></param>指定信息 /// <returns></returns> 返回数组元素为“”则引发了异常 public static string[] GetAlarmMesgArray(string filename, string mesg) { List <string> mydata = new List <string>(); string[] array = new string[100]; try { StreamReader read = new StreamReader(filename, Encoding.Default); string strcontent = read.ReadToEnd(); read.Close(); string[] data = System.Text.RegularExpressions.Regex.Split(strcontent, "\r\n"); for (int j = 1; j < data.Count(); j++) { if (data[j] != "") { mydata.Add(data[j]); } } for (int i = 0; i < mydata.Count(); i++) { string[] mess = mydata[i].Split(','); for (int h = 0; h < mess.Count(); h++) { if (mess[h].Equals(mesg)) { return(mess); } } } } catch (Exception ex) { HintMessageInfo.MessageTopMost("csv文件操作出错:" + ex.Message); } return(array); }
public static void WriteCCDReceiveDate(string WriteString, bool bAppendEnter = false, string path = CConst.SAVE_DATA_PATH) { path = Application.StartupPath + "\\Data"; path = path + CConst.Save_Folder_Name; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } StreamWriter swf = null; string strFile = path + "\\" + DateTime.Now.ToString("yyyyMMdd") + "CCDReceive.csv"; try { //if (swf == null) { swf = new StreamWriter(strFile, true, Encoding.ASCII); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); { swf = new StreamWriter(strFile, true, Encoding.Unicode); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); { swf = new StreamWriter(strFile, true, Encoding.BigEndianUnicode); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); { swf = new StreamWriter(strFile, true, Encoding.UTF32); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); { swf = new StreamWriter(strFile, true, Encoding.UTF7); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); { swf = new StreamWriter(strFile, true, Encoding.UTF8); } if (bAppendEnter) { swf.Write(WriteString + "\r\n"); } else { swf.Write(WriteString); } swf.Close(); } catch (System.Exception ex) { HintMessageInfo.MessageTopMost("csv文件操作出错:" + ex.Message + " 请不要在运行中打开csv文件"); } }