欢迎访问!
您的支持是我们不断前行的动力!

C# 获取指定目录下指定排序方式指定后缀指定数量的文件

/// <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,在搜索操作中包括当前目录和所有子目录。

赞(1) 打赏
转载请注明出处,未注明出处不得转载,蜘蛛侠 » C# 获取指定目录下指定排序方式指定后缀指定数量的文件
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

蜘蛛侠,您身边的爬虫专家!

觉得文章有用就打赏一下文章作者吧

支付宝扫一扫打赏

微信扫一扫打赏