汇总结果时出现并行LINQ UnauthorizedAccessException

时间:2019-03-11 06:56:54

标签: c# linq exception plinq

我正在使用LINQ并行搜索以找到模式匹配文件。

public class ParallelLinq
{
    public IList<string> SearchFolders = new List<string>
    {
        @"C:\Windows" //can be multiple
    };

    protected virtual IEnumerable<string> GetFiles(string path, string[] searchPatterns, SearchOption searchOption = SearchOption.AllDirectories)
    {
        return searchPatterns.AsParallel()
            .SelectMany(searchPattern =>
            {
                try
                {
                    return Directory.EnumerateFiles(path, searchPattern, searchOption);
                }
                catch (Exception ex) //catch UnauthoizedException/IOExceptions
                {
                    return Enumerable.Empty<string>();
                }
            });
    }

    public IEnumerable<string> Find(IList<string> patterns)
    {
        var testResultFiles = Enumerable.Empty<string>();

        if (!SearchFolders.Any() || !patterns.Any())
        {
            return testResultFiles;
        }

        testResultFiles = SearchFolders.AsParallel().Aggregate(testResultFiles, (current, folder) => current.Union(GetFiles(folder, patterns.ToArray())));

        return testResultFiles;
    }
}

但是,当我尝试评估System.UnauthorizedAccessException: Access to the path 'C:\Windows\appcompat\Programs' is denied.的值时

var plinq = new ParallelLinq();
var res = plinq.Find(new List<string> { "*.dll" });
Console.WriteLine("Linq Count: " + res.Count());

虽然预计会出现这些例外情况,但我们如何才能抓住它们并继续前进呢?

完全例外:

  

未处理的异常:System.AggregateException:一个或多个错误   发生。 ---> System.UnauthorizedAccessException:访问路径   'C:\ Windows \ appcompat \ Programs'被拒绝。在   System.IO .__ Error.WinIOError(Int32 errorCode,字符串mayFullPath)
  在   System.IO.FileSystemEnumerableIterator 1.AddSearchableDirsToStack(SearchData localSearchData) at System.IO.FileSystemEnumerableIterator 1.MoveNext()在   System.Linq.Parallel.SelectManyQueryOperator 3.SelectManyQueryOperatorEnumerator 1.MoveNext(TOutput&   currentElement,成对2& currentKey) at System.Linq.Parallel.PipelineSpoolingTask 2.SpoolingWork()在   System.Linq.Parallel.SpoolingTaskBase.Work()在   System.Linq.Parallel.QueryTask.BaseWork(对象未使用)位于   System.Linq.Parallel.QueryTask。<> c。<。cctor> b__10_0(对象o)在   System.Threading.Tasks.Task.InnerInvoke()在   System.Threading.Tasks.Task.Execute()-内部异常结束   堆栈跟踪---   System.Linq.Parallel.QueryTaskGroupState.QueryEnd(布尔   userInitiatedDispose)   System.Linq.Parallel.AsynchronousChannelMergeEnumerator 1.MoveNextSlowPath() at System.Linq.Parallel.AsynchronousChannelMergeEnumerator 1.MoveNext()
  在System.Linq.Parallel.QueryOpeningEnumerator 1.MoveNext() at System.Linq.Enumerable.<UnionIterator>d__67 1.MoveNext()在   System.Linq.Enumerable.Count [TSource](IEnumerable`1源)

2 个答案:

答案 0 :(得分:0)

1. legacy ARM7 interrupts
2. vectored interrupts
3. hardware vectored interrupts

答案 1 :(得分:-1)

似乎路径'C:\ Windows \ appcompat \ Programs'阻止该程序创建文件。可以通过在文件夹本身中添加额外的权限来解决该问题。

How to add permission to folder manually