Click here to Skip to main content
15,896,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How to delete file and folder from main folder in vb.
i have a main folder in that i have some files and inside folder also have some file,so i need to delete in vb.

yybxo3fhzgcojvjj5mqv0mag\sample.txt
yybxo3fhzgcojvjj5mqv0mag\temp\Cache\form mail
yybxo3fhzgcojvjj5mqv0mag\temp\Cache\form mail\2014-07-01T06_22_20\100@678x.Pdf

in above "yybxo3fhzgcojvjj5mqv0mag" is main folder that contain sample.txt file and also temp,Cache,form mail,2014-07-01T06_22_20 contain subfolders,in "2014-07-01T06_22_20" folder contain 100@678x.Pdf file.

so if use like


VB
Try
       Dim dinfo As New DirectoryInfo(Server.MapPath("") & "\yybxo3fhzgcojvjj5mqv0mag")
       If dinfo IsNot Nothing Then
           For Each finfo As FileInfo In dinfo.GetFiles()
               finfo.Delete()
           Next
           dinfo.Delete()
       End If
   Catch ex As Exception

   End Try


it will delete files form main folder only it not delete file form subfolders and also subfolders need to delete.

So if main folder have files and folder ,if i delete main folder only it need to delete main folder,files and subfolders also.

Pls reply asap

Regards

Aravind
Posted

1 solution

VB:
VB
Private Sub DeletFilesAndFolder()
	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



End Sub


C#:
C#
private void DeletFilesAndFolder()
       {
           string path = @"C:\Documents and Settings\prasad\Desktop\folder";

           if (Directory.Exists(path))

               Directory.Delete(path, true);
           else
               Console.WriteLine(path + " not exists");



       }
 
Share this answer
 
Comments
Aravindba 8-Aug-14 4:28am    
Hi Hello,in the description i am clearly explain,inside main folder,have files and subfolders also,if i try ur code i am get this error.
"The directory is not empty."
pls pls read fully and reply

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