title: C# File and Directory iteration, possible to do both at one?
body: <p>This might be a confusing question but I have written below a Directory crawler, that will start at a root crawler, find all unique directories and then find all files and count them and add up their file size.  However, the way I have it written requires going to the directory twice, one to find the directories and the next time to count the files.  If/how is it possible to get all the information once?</p>
  
  <pre><code>       Stopwatch stopwatch = new Stopwatch();
          stopwatch.Start();
          HashSet&lt;string&gt; DirectoryHolding = new HashSet&lt;string&gt;();
          DirectoryHolding.Add(rootDirectory);
  
  
          #region All Directory Region
          int DirectoryCount = 0;
          int DirectoryHop = 0;
          bool FindAllDirectoriesbool = true;
          while (FindAllDirectoriesbool == true)
          {
              string[] DirectoryHolder = Directory.GetDirectories(rootDirectory);
              if (DirectoryHolder.Length == 0)
              {
                  if (DirectoryHop &gt;= DirectoryHolding.Count())
                  {
                      FindAllDirectoriesbool = false;
                  }
                  else
                  {
                      rootDirectory = DirectoryHolding.ElementAt(DirectoryHop);
                  }
                  DirectoryHop++;
              }
              else
              {
                  foreach (string DH in DirectoryHolder)
                  {
                      DirectoryHolding.Add(DH);
                  }
                  if (DirectoryHop &gt; DirectoryHolding.Count())
                  {
                      FindAllDirectoriesbool = false;
                  }
                  rootDirectory = DirectoryHolding.ElementAt(DirectoryHop);
                  DirectoryHop++;
  
              }
  
          }
          DirectoryCount = DirectoryHop - 2;
          #endregion
  
  
  
          #region File Count and Size Region
          int FileCount = 0;
          long FileSize = 0;
          for (int i = 0; i &lt; DirectoryHolding.Count ; i++)
          {
              string[] DirectoryInfo = Directory.GetFiles(DirectoryHolding.ElementAt(i));
              for (int fi = 0; fi &lt; DirectoryInfo.Length; fi++)
              {
                  try
                  {
                      FileInfo fInfo = new FileInfo(DirectoryInfo[fi]);
                      FileCount++;
                      FileSize = FileSize + fInfo.Length;
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message.ToString());
                  }
              }
          }
  </code></pre>
  
  <p>The stopwatch result for this is 1.38</p>
  
  <pre><code>int FileCount = 0;
          long FileSize = 0;
          for (int i = 0; i &lt; DirectoryHolding.Count; i++)
          {
              var entries = new DirectoryInfo(DirectoryHolding.ElementAt(i)).EnumerateFileSystemInfos();
              foreach (var entry in entries)
              {
                  if ((entry.Attributes &amp; FileAttributes.Directory) == FileAttributes.Directory)
                  {
                      DirectoryHolding.Add(entry.FullName);
                  }
                  else
                  {
                      FileCount++;
                      FileSize = FileSize + new FileInfo(entry.FullName).Length;
                  }
              }
          }
  </code></pre>
  
  <p>the stop watch for this method is 2.01,</p>
  
  <p>this makes no sense to me.</p>
  
  <pre><code> DirectoryInfo Dinfo = new DirectoryInfo(rootDirectory);
              DirectoryInfo[] directories = Dinfo.GetDirectories("*.*", SearchOption.AllDirectories);
              FileInfo[] finfo = Dinfo.GetFiles("*.*", SearchOption.AllDirectories);
              foreach (FileInfo f in finfo)
              {
                  FileSize = FileSize + f.Length;
              }
              FileCount = finfo.Length;
              DirectoryCount = directories.Length; 
  </code></pre>
  
  <p>.26 seconds  i think this is the winner</p>