How can I delete a non empty folder ?

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.
 
K

kimiraikkonen

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.
 
L

Lloyd Sheen

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
 
F

Family Tree Mike

Use Directory.Delete(folder, true). This override allows for recursion on
non-empty folders, provided you have permission to do so on all files.
 
B

Bill McCarthy

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.
 
Joined
Jul 29, 2013
Messages
1
Reaction score
0
: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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top