title: How to quickly get a large list of various files from a server?
body: <p>I'm building an application that'll copy files from one server to another. I have these locations mapped as network drives so <code>DeployFrom</code> is something like <code>Z:\MyPath\Subpath</code>.</p>
  
  <p>The actual code to get all the files:</p>
  
  <pre><code>List&lt;string&gt; files = Directory.GetFiles(DeployFrom, "*.aspx", SearchOption.AllDirectories).ToList();
          files.AddRange(Directory.GetFiles(DeployFrom, "*.ascx", SearchOption.AllDirectories));
          files.AddRange(Directory.GetFiles(DeployFrom, "*.css", SearchOption.AllDirectories));
          files.AddRange(Directory.GetFiles(DeployFrom, "*.htm", SearchOption.AllDirectories));
          files.AddRange(Directory.GetFiles(DeployFrom, "*.html", SearchOption.AllDirectories));
          files.AddRange(Directory.GetFiles(DeployFrom, "*.js", SearchOption.AllDirectories));
  </code></pre>
  
  <p>But it runs <em>extremely</em> slow as there are around 2 GB / 13.6K files in <code>DeployFrom</code>.</p>
  
  <p>I found this suggestion in a similar SO post about using <code>GetFiles</code> for multiple types:</p>
  
  <pre><code>Directory.GetFilesDeployFrom, "*.*", SearchOption.AllDirectories)
      .Where(s =&gt; s.EndsWith(".aspx") || s.EndsWith(".css") || s.EndsWith(".htm") || s.EndsWith(".html") || s.EndsWith(".js")).ToList();
  </code></pre>
  
  <p>But this still takes about 2 minutes to build a list of 1800 files.</p>
  
  <p>Is there a faster way, without hardcoding a list of folders to specifically check?</p>
  
  <p>Only other option I can think of is to use <code>Directory.GetDirectories()</code> and filter out a blacklist of folders I know I don't care about, then iterate over that collection and call that second snippet of code above using <code>SearchOption.TopDirectoryOnly</code> instead. I don't want to hardcode "good" folders to check, because if new "good" folders get added then we'll have to come and add them to this utility too. But even still, this won't cut down on the number of files checked very much. Most of the "bad" folders just have large files, which I don't think effects the runtime.</p>