How can I delete a non empty folder ?

  • Thread starter Thread starter fniles
  • Start date Start date
F

fniles

I am using VB.NET 2005.
When I try to delete a folder that has files underneath it, it gave me "the
directory is not empty" error.

f = New IO.DirectoryInfo("C:\myfolder")
If f.Exists Then
f.Delete() --> error "the directory is not empty"
End If

How can I delete a folder with files underneath it ? Thank you.
 
I am using VB.NET 2005.
When I try to delete a folder that has files underneath it, it gave me "the
directory is not empty" error.

f = New IO.DirectoryInfo("C:\myfolder")
If f.Exists Then
    f.Delete()  --> error "the directory is not empty"
End If

How can I delete a folder with files underneath it ? Thank you.

I think it's not a problem about your code, it's about the directory.
I think(not sure) you can't delete this directory manually under
Explorer without your app, neither.

I recommend you to see for "hidden" files in your directory which may
be in use by Windows. Then delete all hidden files, try again.
 
fniles said:
I am using VB.NET 2005.
When I try to delete a folder that has files underneath it, it gave me
"the directory is not empty" error.

f = New IO.DirectoryInfo("C:\myfolder")
If f.Exists Then
f.Delete() --> error "the directory is not empty"
End If

How can I delete a folder with files underneath it ? Thank you.

Do as the message says and delete all the files in the folder. If there are
sub-folders you will have to do the same with them.

LS
 
Use Directory.Delete(folder, true). This override allows for recursion on
non-empty folders, provided you have permission to do so on all files.
 
Try using :
My.Computer.FileSystem.DeleteDirectory(......)

There's a number of overloads there that allow various options including
deleting content, showing progress, using recycle bin etc.
 
:D

I do not know if this will work but it works for me under Visual Basic 2010!

My.Computer.FileSystem.DeleteDirectory("your directory here", FileIO.DeleteDirectoryOption.DeleteAllContents)
 
Back
Top