tags:

views:

389

answers:

6

Hi all,

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 eror "Folder is not empty", any suggestions on what I can do?

Thanks

 try
{
 var dir = new DirectoryInfo(@FolderPath);
 dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
 dir.Delete();
 dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
}
catch (IOException ex)
{
 MessageBox.Show(ex.Message);
}
+6  A: 

Read the Manual:

http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

Directory.Delete(folderPath, true);
dbemerlin
+3  A: 
dir.Delete(true); // true => recursive delete
Tommy Carlier
A: 

The Directory.Delete method has a recursive boolean parameter, it should do what you need

Paolo Tedesco
A: 

Err, what about just calling Directory.Delete(path, true); ?

Dmitri Nesteruk
+1  A: 

Try:

System.IO.Directory.Delete(path,true)

This will recursively delete all files and folders underneath "path" assuming you have the permissions to do so.

jinsungy
A: 

You should use:

dir.Delete(true);

for recursively deleting the contents of that folder too. See MSDN DirectoryInfo.Delete() overloads.

Cloud