Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i delete sub directories

Status
Not open for further replies.

RMasters

Programmer
Nov 14, 2000
36
0
0
GB
I need to set up an app that deletes a folder and all subdirectories, I am using a piece of code that seems to work, except that the sub directories aren't removed from memory (the hard drive stats stay the same). I am assuming that this is not good practice, can anyone help me with the correct way to delete subdirectories. Currently I use this-
Public Function DeleteDirectory(ByVal FullPath As String) _
As Boolean

Dim objDI As New DirectoryInfo(FullPath)
Try
objDI.Delete(True)
Return True
Catch
Return False
End Try
End Function

Thanks for any help
 
RMasters,
I tried to repeat your problem but unable to get the same results.

I do not know if it will change anything if you change the line:
Dim objDI As New DirectoryInfo(FullPath)
to:
Dim objDI As DirectoryInfo = new DirectoryInfo(FullPath)
as this class requires an instance to run.

I am using XP pro and running vb.net 2002.

I have executed the following procedures with a directory tree window side by side, and I see the directories appearing and disappearing. It works also with c:\tmp\tmp having sub-directories.

If you want, you can try my procedures, but before execution you'd have to make a sub-directory called c:\tmp.

Here is the program and hope it helps.

Private Sub Btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
Dim dirTmp As DirectoryInfo
dirTmp = New DirectoryInfo("c:\tmp\tmp\")
If dirTmp.Exists Then
dirTmp.Delete(True)
Else
MsgBox("c:\tmp\tmp\ does not exist")
Exit Sub
End If
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
Dim dirTmp As DirectoryInfo
dirTmp = New DirectoryInfo("c:\tmp\tmp\")
If Not dirTmp.Exists Then

dirTmp.Create()
Else
MsgBox("c:\tmp\tmp\ exists already")
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top