Home All Groups Group Topic Archive Search About

Delete Directory not working

Author
26 Nov 2006 5:41 PM
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?

Author
26 Nov 2006 6:01 PM
Ken Tucker [MVP]
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
-------------------------------
Show quoteHide quote
"Anil Gupte" wrote:

> 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?
> --
> Anil Gupte
> www.keeninc.net
> www.icinema.com
>
>
>
Author
26 Nov 2006 6:27 PM
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....

Show quoteHide quote
"Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message
news:4E5218A4-8924-4DFC-9994-7943D815BCE9@microsoft.com...
> 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
> -------------------------------
> "Anil Gupte" wrote:
>
>> 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?
>> --
>> Anil Gupte
>> www.keeninc.net
>> www.icinema.com
>>
>>
>>
Author
26 Nov 2006 11:15 PM
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!

--
"Anil Gupte" <anil-l***@icinema.com> wrote in message
news:OSVb%23hYEHHA.2328@TK2MSFTNGP02.phx.gbl...
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....

Show quoteHide quote
"Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message
news:4E5218A4-8924-4DFC-9994-7943D815BCE9@microsoft.com...
> 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
> -------------------------------
> "Anil Gupte" wrote:
>
>> 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?
>> --
>> Anil Gupte
>> www.keeninc.net
>> www.icinema.com
>>
>>
>>
Author
27 Nov 2006 12:12 AM
al jones
On Sun, 26 Nov 2006 23:15:10 GMT, Ryan S. Thiele wrote:

Show quoteHide quote
> 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!
>
> --
> "Anil Gupte" <anil-l***@icinema.com> wrote in message
> news:OSVb%23hYEHHA.2328@TK2MSFTNGP02.phx.gbl...
> 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
Author
27 Nov 2006 6:12 AM
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.

Show quoteHide quote
"al jones" <alfredmjo***@shotmail.com> wrote in message
news:8er1vt7cnvkr$.a2ar234awy5w$.dlg@40tude.net...
> On Sun, 26 Nov 2006 23:15:10 GMT, Ryan S. Thiele wrote:
>
>> 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!
>>
>> --
>> "Anil Gupte" <anil-l***@icinema.com> wrote in message
>> news:OSVb%23hYEHHA.2328@TK2MSFTNGP02.phx.gbl...
>> 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
Author
27 Nov 2006 11:30 PM
al jones
On Mon, 27 Nov 2006 11:42:13 +0530, Anil Gupte wrote:

> 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
Author
26 Nov 2006 7:22 PM
Franky
Would this still work if there were directories in the directory?


Show quoteHide quote
"Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message
news:4E5218A4-8924-4DFC-9994-7943D815BCE9@microsoft.com...
> 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
> -------------------------------
> "Anil Gupte" wrote:
>
>> 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?
>> --
>> Anil Gupte
>> www.keeninc.net
>> www.icinema.com
>>
>>
>>