title: Get all files and directories in specific path fast
body: <p>I am creating a backup application where c# scans a directory. Before I use to have something like this in order to get all the files and subfiles in a directory:</p>
  
  <pre><code>DirectoryInfo di = new DirectoryInfo("A:\\");
  var directories= di.GetFiles("*", SearchOption.AllDirectories);
  
  foreach (FileInfo d in directories)
  {
         //Add files to a list so that later they can be compared to see if each file
         // needs to be copid or not
  }
  </code></pre>
  
  <p>The only problem with that is that sometimes a file could not be accessed and I get several errors. an example of an error that I get is:<img src="https://i.stack.imgur.com/5YH4D.png" alt="error"></p>
  
  <p>As a result I created a recursive method that will scan all files in the current directory. If there where directories in that directory then the method will be called again passing that directory. The nice thing about this method is that I could place the files inside a try catch block giving me the option to add those files to a List if there where no errors and adding the directory to another list if I had errors.</p>
  
  <pre><code>try
  {
      files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);               
  }
  catch
  {
       //info of this folder was not able to get
       lstFilesErrors.Add(sDir(di));
       return;
  }
  </code></pre>
  
  <p>So this method works great the only problem is that when I scan a large directory it takes to much times. How could I speed up this process?   My actual method is this in case you need it. </p>
  
  <pre><code>private void startScan(DirectoryInfo di)
  {
      //lstFilesErrors is a list of MyFile objects
      // I created that class because I wanted to store more specific information
      // about a file such as its comparePath name and other properties that I need 
      // in order to compare it with another list
  
      // lstFiles is a list of MyFile objects that store all the files
      // that are contained in path that I want to scan
  
      FileInfo[] files = null;
      DirectoryInfo[] directories = null;
      string searchPattern = "*.*";
  
      try
      {
          files = di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);               
      }
      catch
      {
          //info of this folder was not able to get
          lstFilesErrors.Add(sDir(di));
          return;
      }
  
      // if there are files in the directory then add those files to the list
      if (files != null)
      {
          foreach (FileInfo f in files)
          {
              lstFiles.Add(sFile(f));
          }
      }
  
  
      try
      {
          directories = di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);
      }
      catch
      {
          lstFilesErrors.Add(sDir(di));
          return;
      }
  
      // if that directory has more directories then add them to the list then 
      // execute this function
      if (directories != null)
          foreach (DirectoryInfo d in directories)
          {
              FileInfo[] subFiles = null;
              DirectoryInfo[] subDir = null;
  
              bool isThereAnError = false;
  
              try
              {
                  subFiles = d.GetFiles();
                  subDir = d.GetDirectories();
  
              }
              catch
              {
                  isThereAnError = true;                                                
              }
  
              if (isThereAnError)
                  lstFilesErrors.Add(sDir(d));
              else
              {
                  lstFiles.Add(sDir(d));
                  startScan(d);
              }
  
  
          }
  
  }
  </code></pre>
  
  <p>Ant the problem if I try to handle the exception with something like:</p>
  
  <pre><code>DirectoryInfo di = new DirectoryInfo("A:\\");
  FileInfo[] directories = null;
              try
              {
                  directories = di.GetFiles("*", SearchOption.AllDirectories);
  
              }
              catch (UnauthorizedAccessException e)
              {
                  Console.WriteLine("There was an error with UnauthorizedAccessException");
              }
              catch
              {
                  Console.WriteLine("There was antother error");
              }
  </code></pre>
  
  <p>Is that if an exception occurs then I get no files.</p>