I am searching in parallel using LINQ
to find pattern matching files.
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;
}
}
However when I try evaluate the values I am running into 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());
While these exceptions are expected, How can we catch them and continue ahead?
Full Exception:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.UnauthorizedAccessException: Access to the path 'C:\Windows\appcompat\Programs' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator1.AddSearchableDirsToStack(SearchData localSearchData) at System.IO.FileSystemEnumerableIterator
1.MoveNext() at System.Linq.Parallel.SelectManyQueryOperator3.SelectManyQueryOperatorEnumerator
1.MoveNext(TOutput& currentElement, Pair2& currentKey) at System.Linq.Parallel.PipelineSpoolingTask
2.SpoolingWork() at System.Linq.Parallel.SpoolingTaskBase.Work() at System.Linq.Parallel.QueryTask.BaseWork(Object unused) at System.Linq.Parallel.QueryTask.<>c.<.cctor>b__10_0(Object o) at System.Threading.Tasks.Task.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of inner exception stack trace --- at System.Linq.Parallel.QueryTaskGroupState.QueryEnd(Boolean userInitiatedDispose) at System.Linq.Parallel.AsynchronousChannelMergeEnumerator1.MoveNextSlowPath() at System.Linq.Parallel.AsynchronousChannelMergeEnumerator
1.MoveNext()
at System.Linq.Parallel.QueryOpeningEnumerator1.MoveNext() at System.Linq.Enumerable.<UnionIterator>d__67
1.MoveNext() at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)