/// <summary> /// 按创建时间降序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByCreationTimeDesc(string path,string searchPattern,SearchOption searchOption,int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.CreationTime descending select fi.FullName).Take(count); return query.ToArray(); } /// <summary> /// 按创建时间升序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByCreationTimeAsc(string path, string searchPattern, SearchOption searchOption, int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.CreationTime ascending select fi.FullName).Take(count); return query.ToArray(); } /// <summary> /// 按修改时间降序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByLastWriteTimeDesc(string path, string searchPattern, SearchOption searchOption, int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.CreationTime descending select fi.FullName).Take(count); return query.ToArray(); } /// <summary> /// 按修改时间升序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByLastWriteTimeAsc(string path, string searchPattern, SearchOption searchOption, int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.CreationTime ascending select fi.FullName).Take(count); return query.ToArray(); } /// <summary> /// 按文件名称降序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByNameDesc(string path, string searchPattern, SearchOption searchOption, int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.Name ascending select fi.FullName).Take(count); return query.ToArray(); } /// <summary> /// 按文件名称升序读取指定数目,指定后缀,指定目录下的文件 /// </summary> /// <param name="path"></param> /// <param name="searchPattern"></param> /// <param name="searchOption"></param> /// <param name="count"></param> /// <returns></returns> public static string[] GetFilesByNameAsc(string path, string searchPattern, SearchOption searchOption, int count) { var query = (from f in Directory.GetFiles(path, searchPattern, searchOption) let fi = new FileInfo(f) orderby fi.Name ascending select fi.FullName).Take(count); return query.ToArray(); }
SearchOption searchOption解释:
searchOption=0,只在当前目录搜索;
searchOption=1,在搜索操作中包括当前目录和所有子目录。
评论前必须登录!
注册