title: Handle (skip) UnauthorizedAccessException in EnumerateFiles() LINQ C#
body: <p>I am trying to write a high performance file system searcher that can search unindexed drives (both local and network) very fast filtering on extensions and keywords. I am trying to achieve this using C#'s <code>DirectoryInfo.EnumerateDirectories()</code>, <code>DirectoryInfo.EnumerateFiles()</code> and LINQ queries. From my testing, this is (by far) the best performing code I could find: </p>
  
  <pre><code>
  FileInfo[] dirFiles = dirInfo.EnumerateDirectories()
                              .AsParallel()
                              .SelectMany(di =&gt; di.EnumerateFiles("*.*", SearchOption.AllDirectories)
                                                  .Where(fi =&gt; EndsWithExtension(fi.Extension)) )
                                                  .ToArray();
  </code></pre>
  
  <p>However, the <code>UnauthorizedAccessException</code> is not handled and when thrown, crashes the entire query.</p>
  
  <p>I've tried various ways as outlined on SO related to this issue, but I found that they are significantly slower in search performance. This second best method I found working is over 20 times slower for example: </p>
  
  <pre><code>try {
      foreach (string fileName in EnumerateFiles(dirInfo, "*.*", SearchOption.AllDirectories)) {
          if (ContainsKeyword(fileName)) {
               Results.Add(fileName.FullName);
          }
      }
  } catch (Exception e) { continue; }
  
  </code></pre>
  
  <p>I would like to skip over the directory when it throws an exception. I've being trying to achieve this with something similar to this, but I can't get it to work (my knowledge of LINQ and Enumerables is too limited...): </p>
  
  <pre><code>FileInfo[] dirFiles = dirInfo.EnumerateDirectories()
                              .AsParallel()
                              .SelectMany(di =&gt; di.EnumerateFiles("*.*", SearchOption.AllDirectories)
                                                  .SkipExceptions()
                                                  .Where(fi =&gt; EndsWithExtension(fi.Extension)) )
                                                  .ToArray();
  
  public static class Extensions {
          public static IEnumerable&lt;T&gt; SkipExceptions&lt;T&gt;(this IEnumerable&lt;T&gt; values) {
              using (var enumerator = values.GetEnumerator()) {
                  bool next = true;
                  while (next) {
                      try {
                          if (enumerator.Current != null)
                              Console.WriteLine(enumerator.Current.ToString()); 
                          next = enumerator.MoveNext();
                      } catch {
                          continue;
                      }
  
                      if (next) yield return enumerator.Current;
                  }
              }
          }
      }
  </code></pre>
  
  <p>Is it possible to handle (<code>UnauthorizedAccess</code>) exceptions, while still remaining as high performant as the "raw" LINQ query?</p>
  
  <p>Thanks in advance for your help!</p>