2

我有 ASP.NET (C#) 4.0。

我的代码是这样的:

public IEnumerable<string> GetFiles(string searchPatternExpression)
{
    string path = @"D:\xxx\";
    SearchOption searchOption = SearchOption.TopDirectoryOnly;
    Regex reSearchPattern = new Regex(searchPatternExpression);
    return Directory
        .EnumerateFiles(path, "*", searchOption)
        .Where(file => reSearchPattern.IsMatch(Path.GetFileName(file)))
        .OrderBy(file => file);
}

如何按文件的创建日期排序?

4

2 回答 2

7

因此您需要获取文件创建时间,最好使用DirectoryInfo获取FileInfos 而不是简单的字符串:

public IEnumerable<string> GetFiles(string searchPatternExpression)
{
    string path = @"D:\xxx\";
    SearchOption searchOption = SearchOption.TopDirectoryOnly;
    Regex reSearchPattern = new Regex(searchPatternExpression);
    return new DirectoryInfo(path)
        .EnumerateFiles("*", searchOption)
        .Where(file => reSearchPattern.IsMatch(file.Name))
        .OrderBy(file => file.CreationTime)
        .Select(file => file.FullName); // return just names
}
于 2013-09-25T14:09:12.060 回答
0

尝试这个:

public IEnumerable<string> GetFiles(string searchPatternExpression)
{
    string path = @"D:\xxx\";
    SearchOption searchOption = SearchOption.TopDirectoryOnly;
    Regex reSearchPattern = new Regex(searchPatternExpression);
    return Directory
        .EnumerateFiles(path, "*", searchOption)
        .Where(file => reSearchPattern.IsMatch(Path.GetFileName(file)))
        .OrderBy(file => new FileInfo(file).CreationTime);
}
于 2013-09-25T14:04:03.280 回答