title: How do I prevent System.IO.Directory.Delete(pathtodelete, true) from giving me a "Directory is not empty" error?
body: <p>I'm kinda new to working with C# .NET's System.IO namespace. So please forgive me for some basic questions.</p>
  
  <p>I am writing an online interface that will allow a site owner to modify files and directories on the server.</p>
  
  <p>I have gotten inconsistent performance out of <code>System.IO.Directory.Delete(PathToDelete, true);</code>. Sometimes it works great, sometimes it throws an error. My controller looks like this:</p>
  
  <pre><code>    public ActionResult FileDelete(List&lt;string&gt; entity = null)
      {
  
          if (entity != null)
          {
              if (entity.Count() &gt; 0)
  
              foreach (string s in entity)
              {
                  string CurrentFile = s.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
                  string FileToDelete = Server.MapPath(CurrentFile);
  
                  bool isDir = (System.IO.File.GetAttributes(FileToDelete) &amp; FileAttributes.Directory) == FileAttributes.Directory;
  
                  if (isDir)
                  {
                      if (System.IO.Directory.Exists(FileToDelete))
                      {
                          //Problem line/////////////////////////////////
                          System.IO.Directory.Delete(FileToDelete, true);
                      }
                  }
                  else
                  {
                      if (System.IO.File.Exists(FileToDelete))
                      {
                          System.IO.File.Delete(FileToDelete);
  
                          string ThumbConfigDir = ConfigurationManager.AppSettings["ThumbnailSubdirectory"];
                          string ThumbFileToDelete = Path.GetDirectoryName(FileToDelete) + Path.DirectorySeparatorChar + ThumbConfigDir + Path.DirectorySeparatorChar + Path.GetFileName(FileToDelete);
  
                          if (System.IO.File.Exists(ThumbFileToDelete))
                          {
                              System.IO.File.Delete(ThumbFileToDelete);
  
                          }
                      }
                  }
              }
          }
  
          return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri.ToString());
  
      }
  </code></pre>
  
  <p>Sometimes, I get an error when tring to delete directories that says: </p>
  
  <pre><code>The directory is not empty.
  
  Description: An unhandled exception occurred during the execution of the current 
  web request. Please review the stack trace for more information about the error 
  and where it originated in the code. 
  
  
  Exception Details: System.IO.IOException: The directory is not empty.
  
  Source Error: 
  
  Line 137:              if (System.IO.Directory.Exists(FileToDelete))
  Line 138:              {
  Line 139:                  System.IO.Directory.Delete(FileToDelete, true);
  Line 140:              }
  Line 141:          }
  </code></pre>
  
  <p>I'm not sure what kind of defensive coding I can implement to avoid get errors like these. Any thoughts? Am I missunderstanding what it means to set recursive to true by saying <code>System.IO.Directory.Delete(FileToDelete, true);</code>?</p>