title: C# delete a folder and all files and folders within that folder
body: <p>I'm trying to delete a folder and all files and folders within that folder, I'm using the code below and I get the error <code>Folder is not empty</code>, any suggestions on what I can do?</p>
  
  <pre><code>try
  {
    var dir = new DirectoryInfo(@FolderPath);
    dir.Attributes = dir.Attributes &amp; ~FileAttributes.ReadOnly;
    dir.Delete();
    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
  }
  catch (IOException ex)
  {
    MessageBox.Show(ex.Message);
  }
  </code></pre>