Delete Directory not working

A

Anil Gupte

Private Sub mnu2Exit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles mnu2Exit.Click

Dim fDir As String = Path.GetDirectoryName(L3Global.VideoFileName)

File.Delete(L3Global.VideoFileName)

' The following is not working - reports directory not empty exception

' Directory.Delete(fDir)

Application.Exit()

End Sub

There is only one file in there. Once the app exits, I see that the
directory is empty. Perhaps the file takes a while to delete or the
application is still using it until it exits. How can I make sure the
directory is also delete.

Thoughts?
 
G

Guest

Hi,

Try something like this. Note I am assuming there are only fno sub
directories in the directory you are trying to delete.

Dim di As New IO.DirectoryInfo("YourPath")
Dim Files() As IO.FileInfo = di.GetFiles
For x As Integer = Files.GetUpperBound(0) To 0 Step -1
Files(x).Delete()
Next
IO.Directory.Delete("YourPath")

Ken
 
A

Anil Gupte

Interestingly, I did try that, but that gave the exact same results. Except
I did
For Each fileName In fileEntries
filename.delete()
next
something simialr anyway, I erased that chuck of code.

It so happens that the file I am deleting is a Windows Media file, and I can
hear it playing until the applicaiton exists. I can't force users to close
the file before they exit....
 
R

Ryan S. Thiele

You can try to copy the file playing. Then when the program exits, it erases
the temp folder. I don't know if the file.delete/directory.delete command
deletes files in use.

--
Thiele Enterprises - The Power Is In Your Hands Now!

--
Interestingly, I did try that, but that gave the exact same results. Except
I did
For Each fileName In fileEntries
filename.delete()
next
something simialr anyway, I erased that chuck of code.

It so happens that the file I am deleting is a Windows Media file, and I can
hear it playing until the applicaiton exists. I can't force users to close
the file before they exit....
 
A

al jones

You can try to copy the file playing. Then when the program exits, it erases
the temp folder. I don't know if the file.delete/directory.delete command
deletes files in use.

--
Thiele Enterprises - The Power Is In Your Hands Now!

--
Interestingly, I did try that, but that gave the exact same results. Except
I did
For Each fileName In fileEntries
filename.delete()
next
something simialr anyway, I erased that chuck of code.

It so happens that the file I am deleting is a Windows Media file, and I can
hear it playing until the applicaiton exists. I can't force users to close
the file before they exit....

For a fact, from experience, it will NOT delete an in-use file. It should
be throwing an exeption if the OP is checking for that.

//al
 
A

Anil Gupte

Actually this is a temp file. I copy the original, which is inside a zip to
a temp.wmv file. So you say it will not delete a file in use, but it is
deleting it, and I can hear it playing (in fact I hit exit while it is
playing). However, when it gets to the new instruction to delete the
directory it says the directory is not empty and throws an exception with
that message. When the app exits, the file has been deleted.
 
A

al jones

Actually this is a temp file. I copy the original, which is inside a zip to
a temp.wmv file. So you say it will not delete a file in use, but it is
deleting it, and I can hear it playing (in fact I hit exit while it is
playing). However, when it gets to the new instruction to delete the
directory it says the directory is not empty and throws an exception with
that message. When the app exits, the file has been deleted.

I know I'll get corrected if I'm wrong, and welcome that, but I'd suggest
that since you're able to delete the file while it's still playing that WMP
(or whatever you're playing it with) already has it completely buffered so
that it's not, in fact, in use.

//al
 

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