title: How can I recursively count files and folders in Windows with C#?
body: <p><em><strong>EDIT 8/8/2012:</em></strong> <em>I've made some significant changes to the code I'm using and would like some fresh help on one last problem I'm having.  I'm going to rewrite most of this question.</em></p>
  
  <p>I have a small program which iterates recursively through each file and folder under a target directory checking the names for specific characters.  It works just fine but I'm looking for help on how to make a specific method work faster.</p>
  
  <p>Here's the code I'm currently using.  This is just a couple of lines from the method that kicks everything off:</p>
  
  <pre><code>if(getFullList(initialPathTB.Text))
              SearchFolder();  
  </code></pre>
  
  <p>And these are the two methods you need to see:</p>
  
  <pre><code>private void SearchFolder()
  {
      int      newRow;
      int      numItems = 0;
  
      numItems = itemsMaster.Length;
  
      for (int x = 0; x &lt; numItems; x++)
      {
          if (hasIllegalChars(itemsMaster[x]) == true)
          {
              newRow = dataGridView1.Rows.Add();
              dataGridView1.Rows[newRow].Cells[0].Value = itemsMaster[x];
              filesFound++;
          }
      }
  }
  
  private bool getFullList(string folderPath)
  {
      try
      {
          if (checkBox17.Checked)
              itemsMaster = Directory.GetFileSystemEntries(folderPath, "*", SearchOption.AllDirectories);
          else
              itemsMaster = Directory.GetFileSystemEntries(folderPath, "*", SearchOption.TopDirectoryOnly);
          return true;
      }
      catch (UnauthorizedAccessException e)
      {
          if(folderPath[folderPath.Length - 1] != '\\')
              folderPath += @"\";
          if (e.Message == "Access to the path '" + folderPath + "' is denied.")
          {
              MessageBox.Show("You do not have read permission for the following directory:\n\n\t" + folderPath + "\n\nPlease select another folder or log in as a user with read access to this folder.", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
              folderPath = folderPath.Substring(0, folderPath.Length - 1);
          }
          else
          {
              if (accessDenied == null)
                  accessDenied = new StringBuilder("");
              accessDenied.AppendLine(e.Message.Substring(20, e.Message.Length - 32));
          }
          return false;
      }
  }
  </code></pre>
  
  <p><code>initialPathTB.Text</code> is populated with something like "F:\COMMON\Administration".</p>
  
  <p><strong>Here's my problem.</strong>  When the top level that's passed to <code>folderPath</code> is one which the user does not have read access everything works fine.  When the top level <em>and all subordinate directories</em> are folders which the user has read access everything works fine again.  The issue lies with directories where the user has read access to the top level but does not for some child folder deeper within.  This is why <code>getFullList()</code> is a bool; if there are any UnauthorizedAccessExceptions then <code>itemsMaster</code> remains empty and <code>SearchFolder()</code> fails on <code>numItems = itemsMaster.Length;</code>.</p>
  
  <p>What I'd like is to populate <code>itemsMaster</code> with every item within <code>folderPath</code> and simply skip the items for which the user doesn't have read access but I don't know how to do that without recursively crawling and checking each directory.</p>
  
  <p>This code works <em>so much faster</em> than my old method so I'd rather not abandon it for something else entirely.  Is there any way to make the <code>Directory.GetFileSystemEntries()</code> method do what I want?</p>