/// <summary> /// sort the file list based on the given order type /// </summary> /// <param name="fileList"></param> /// <param name="fileOrderType"></param> /// <returns></returns> public static string[] OrderFileBy(string[] fileList, FileOrderType fileOrderType) { string[] orderKey = new string[fileList.Length]; string[] orderVal = new string[fileList.Length]; //int maskLength = StringUtil.MaxLength(fileList); int maskLength = 100; string maskFormat = String.Format(@"{{0,{0}}}", maskLength); for (int i = 0; i < fileList.Length; i++) { string filename = fileList[i]; string orderByKey; if (!File.Exists(filename)) { throw new FileNotFoundException(filename); } FileInfo fi = new FileInfo(filename); switch (fileOrderType) { case FileOrderType.None: orderByKey = ""; break; case FileOrderType.Filename: { orderByKey = String.Format(maskFormat, fi.Name); } break; case FileOrderType.LastWriteTime: { DateTime dt = fi.LastWriteTime; orderByKey = String.Format("{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}{6:000}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond ); } break; default: throw new ArgumentOutOfRangeException("not supported: " + fileOrderType); } orderKey[i] = orderByKey; orderVal[i] = fileList[i]; } if (fileOrderType != FileOrderType.None) { Array.Sort(orderKey, orderVal); } return(orderVal); }
/// <summary> /// Return a list of filenames /// </summary> /// <param name="path"></param> /// <param name="patterns"></param> /// <param name="searchOption"></param> /// <param name="relativePath"></param> /// <param name="fileOrderType"></param> /// <returns></returns> public static string[] GetFiles(string path, string patterns, SearchOption searchOption, bool relativePath, FileOrderType fileOrderType) { List <String> results = new List <string>(); string[] patternList = SplitExtension(patterns); // handle multiple patterns, such as "*.AAA,*.BBB,*.CCC" for (int j = 0; j < patternList.Length; j++) { results.AddRange(Directory.GetFiles(path, patternList[j], searchOption)); } // apply order by ... string[] orderList = OrderFileBy(results.ToArray(), fileOrderType); // apply relative path ... if (!relativePath) { return(orderList); } else { return(MakeRelativePath(orderList, path)); } }