public static string[] GetFileNames(string path, string format, TxtDateType type) { var res = new List <string>(); try { string[] files = Directory.GetFiles(path, format, SearchOption.TopDirectoryOnly); //查找根目录下的所有保存的振动数据"*.usb" foreach (string fileName in files) { try { File.Open(fileName, FileMode.Open, FileAccess.ReadWrite).Close(); //文件正在被写入,无法提取 res.Add(fileName); } catch { } } return(res.ToArray()); } catch { return(new string[] {}); } }
///// <summary> ///// 根据文件名读取参数 ///// </summary> ///// <param name="filename"></param> ///// <returns></returns> //public FileParam ReadParamFromFileName(string filename) //{ // string file = Path.Combine(FilePath, filename + @".sdb"); // return ReadParamS(file); //} /// <summary> /// 获取文件列表 /// </summary> /// <param name="path"></param> /// <param name="format"></param> /// <param name="time"></param> /// <param name="type"></param> /// <returns></returns> private string[][] GetFileNames(string path, string format, TxtDateType type) { var paras = new List <string>(); var res = new List <string>(); //根据时间从小到大排序 var tempList = new SortedList <DateTime, List <string> >(); string[] files = Directory.GetFiles(path, format, SearchOption.TopDirectoryOnly); foreach (string file in files) { if (file.EndsWith("odb") || file.EndsWith("sdb")) { if (!this._isExtractTimeSamplingData) { if (!file.Contains("_TRI")) { continue; } } try { DateTime recordTime = GetFileRecordTime(file, TxtDateType.Vibration); //if (DateTime.Compare(recordTime, time) > 0) //{ File.Open(file, FileMode.Open, FileAccess.ReadWrite).Close(); if (!tempList.ContainsKey(recordTime)) { tempList.Add(recordTime, new List <string>()); } tempList[recordTime].Add(file); // } } catch { } } } foreach (DateTime key in tempList.Keys) { foreach (string file in tempList[key]) { if (file.EndsWith("odb")) { res.Add(file); } else if (file.EndsWith("sdb")) { paras.Add(file); } } } return(new[] { res.ToArray(), paras.ToArray() }); }
/// <summary> /// 获取文件的最后写入时间 /// </summary> /// <param name="filename"></param> /// <param name="type"></param> /// <returns></returns> private DateTime GetFileRecordTime(string filename, TxtDateType type) { var fi = new FileInfo(filename); // DateTime readtime; // ValueHelper.String2Time(fi.LastWriteTime.ToString("yyyyMMddHHmmss.fff"), out readtime); return(fi.LastWriteTime); //string[] timerstrs = filename.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries); //if (timerstrs.Length >= 3) //{ // DateTime time; // if (ValueHelper.String2Time(timerstrs[2], out time)) // { // return time; // } // throw new Exception("时间格式错误!"); //} //throw new Exception("时间格式错误!"); }
public static DateTime GetFileRecordTime(string filepath, TxtDateType type) { DateTime time; switch (type) { case TxtDateType.Vibration: return(DateTime.ParseExact( Path.GetFileNameWithoutExtension(filepath).Remove(0, 4), "yyyyMMddHHmmss", null)); //case TxtDateType.FiberGrating: // return DateTime.ParseExact( // Path.GetFileNameWithoutExtension(filepath).Remove(0, 4), // "yyyyMMddHHmmss", // null); default: return(new DateTime(2013, 1, 1)); break; } }