title: Delete Folder if it is Empty using C#
body: <p>I am writing a tool that will allow me to go though a fairly large list of Directories and Sub-directories. I would like it to delete a folder if there it is empty. I can delete folders and sub folders that are empty with this code: </p>
  
  <pre><code>string dir = textBox1.Text;
  string[] folders = System.IO.Directory.GetDirectories(dir, "*.*", System.IO.SearchOption.AllDirectories);
  foreach (var directory in folders)
  {
      if (System.IO.Directory.GetFiles(directory).Length == 0 &amp;&amp; System.IO.Directory.GetDirectories(directory).Length == 0)
      {
          System.IO.StreamWriter Dfile = new System.IO.StreamWriter(newpath, true);
          System.IO.Directory.Delete(directory);
      }
  }
  </code></pre>
  
  <p>My question is how to have the code go though and check the folders after each delete because once it deletes a folder it could make the parent folder empty and should then should be deleted. Once the code does not find any folders or sub-folders that are empty it would exit.</p>