将IEnumerable <string>转换为IEnumerable <fileinfo> </fileinfo> </string>

时间:2014-06-03 12:00:09

标签: c# .net linq

有没有办法让IEnumerable<FileInfo>退出Directory.EnumerateDirectories(..)

我的方法看起来像这样。

public static IEnumerable<String> EnumerateFiles(String path, String searchPattern, SearchOption searchOpt)
{
    try
    {
        IEnumerable<string> dirFiles = Enumerable.Empty<String>();
        if (searchOpt == SearchOption.AllDirectories)
        {
            dirFiles = Directory.EnumerateDirectories(path)
                                .SelectMany(x => EnumerateFiles(x, searchPattern, searchOpt));
        }
        return dirFiles.Concat(Directory.EnumerateFiles(path, searchPattern));
    }
    catch (UnauthorizedAccessException ex)
    {
        return Enumerable.Empty<String>();
    }
}

我也想实施:

public static IEnumerable<FileInfo> EnumerateFiles(String path, String searchPattern, SearchOption searchOpt)
{
  //...code
}

1 个答案:

答案 0 :(得分:1)

尝试使用DirectoryInfo.EnumerateFiles代替