1

我尝试使用递归内置方法:

public IEnumerable<string> Foo(string path)
{
    try
    {
        foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
        {
            yield return file;
        }
    }
    catch (Exception)
    { }
}

并收到此错误:

无法在带有 catch 子句的 try 块的主体中​​产生值

try-catch如果我没有文件夹权限,我该如何使用以避免我的方法崩溃?

4

1 回答 1

1
public IEnumerable<string> Foo(string path)
{
    var files = new List<string>();
    try
    {
      files.AddRange(Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories));
    }
    catch (Exception)
    {
       // this is bad style. You should catch more specific exceptions
    }

    return files;
}
于 2013-05-26T09:29:46.723 回答