Click here to Skip to main content
15,358,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am try to delete main folder which contain files and subfolders,using this code

VB
Dim path As String = "C:\Documents and Settings\prasad\Desktop\folder"
 
	If Directory.Exists(path) Then
 
		Directory.Delete(path, True)
	Else
		Console.WriteLine(path & " not exists")
	End If


E.G.in above code folder have files and also subfolder like
C:\Documents and Settings\prasad\Desktop\folder\one.doc
C:\Documents and Settings\prasad\Desktop\folder\foldler2\ddd.docx
C:\Documents and Settings\prasad\Desktop\folder\fol\fff.txt

If i try above vb code i will get this error "The directory is not empty"
So pls reply me how to delete main folder which contains files and subfolder.

Note:Main folder contains files and subfolder,i am dont want like first delete files and then subfolder files and then subfolder form main folder and finally main folder.

I need to delete main folder only,then automatically delete its files and subfolder.like how,if we select and right click the main folder and click delete by mouse.

Regards
Aravind
Posted
Updated 11-May-22 0:20am

Have a look at: Directory.Delete Method (String, Boolean)[^], see the caode sample for deleting recursively a folder.
   
v2
Hi I solved this one by getting solution in microsoft forums,Below i post that codes in vb language with description

VB
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnClean.Click

       'Set variable di as the path you wish to clean
       Dim di As New DirectoryInfo("C:\Windows\Temp")
       tbLocationbeingcleaned.Text = di.ToString()

       'Traverse all of the child directors in the root; get to the lowest child and delete all files, working our way back up to the top.
       'All files must be deleted in the directory, before the directory itself can be deleted.
       For Each diChild As DirectoryInfo In di.GetDirectories()
           TraverseDirectory(diChild)
       Next

       'Finally, call the routine to clean all of the files directly in the root directory
       CleanAllFilesInDirectory(di)

          End Sub

'Routine to traverse through directories and sub-directories - it passes over to cleanall routine to delete files and then deletes the empty directory
   Private Sub TraverseDirectory(ByVal di As DirectoryInfo)
       'If the current directory has more child directories, then continue to traverse down until we are at the lowest level. At that point all of the files will be deleted.
       For Each diChild As DirectoryInfo In di.GetDirectories()
           Try
               TraverseDirectory(diChild)
           Catch ex As Exception
               '   lbErrors.Items.Add(diChild.ToString() & " = " & ex.Message)
           End Try
       Next
       'Now that we have no more child directories to traverse, delete all of the files in the current directory, and then delete the directory itself.
       CleanAllFilesInDirectory(di)
       'The containing directory can only be deleted if the directory is now completely empty and all files previously within were deleted.
       If di.GetFiles().Count = 0 Then
           Try
               di.Delete()
           Catch ex As Exception
               ' lbErrors.Items.Add(di.ToString() & " = " & ex.Message)
           End Try
       End If
   End Sub

   'Routine to delete all files in current directory - is called by traversedirectory routine above
   Private Sub CleanAllFilesInDirectory(ByVal DirectoryToClean As DirectoryInfo)
       For Each fi As FileInfo In DirectoryToClean.GetFiles()
           Try
               'Read only files can not be deleted, so mark the attribute as 'IsReadOnly = False'
               fi.IsReadOnly = False
               fi.Delete()
               'Sometimes files being deleted might be slower than the program execution, and upon returning
               'from this call, attempting to delete the directory will throw an exception stating it is not yet
               'empty, even though a fraction of a second later it actually is. Therefore the code below
               'pauses the process just long enough to ensure the file is deleted before proceeding. The value
               'can be adjusted as needed from testing and running the process repeatedly.
               'System.Threading.Thread.Sleep(50) '50 millisecond stall (0.05 Seconds)
           Catch ex As Exception
               ' lbErrors.Items.Add(fi.ToString() & " = " & ex.Message)
           End Try
       Next
   End Sub



And for ur clarification u can refer this link

http://social.msdn.microsoft.com/Forums/silverlight/en-US/6eac546b-6663-4c36-aa53-44f44a15db33/deleting-contents-of-a-folder-including-all-subfolders-and-files?forum=vblanguage[^]


Regards
Aravind
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900