如何删除它包含文件和子文件夹的主文件夹? [英] How to delete main folder which it contains files and subfolder ?
问题描述
嗨
我尝试删除包含文件和子文件夹的主文件夹,使用此代码
Hi
I am try to delete main folder which contain files and subfolders,using this code
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
EGin上面的代码文件夹中有文件,还有子文件夹,如
C:\Documents and Settings\prasad\Desktop\folder\ on e.doc
C:\Documents and Settings\prasad \Desktop\folder\foldler2 \ddd.docx
C:\ Document and Settings \ prasad \Desktop\folder\fol\fff.txt
如果我尝试上面的vb代码,我会收到此错误该目录不为空
所以请回复我如何删除包含文件和子文件夹的主文件夹。
注意:主文件夹包含文件和子文件夹,我我不想首先删除文件然后再删除子文件夹文件然后子文件夹形成主文件夹,最后是主文件夹。
我只需要删除主文件夹,然后自动删除它文件和子文件夹。如何,如果我们选择并右键单击主文件夹并单击鼠标删除。
问候
Aravind
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
推荐答案
看看: Directory.Delete方法( String,Boolean) [ ^ ],请参阅caode示例以递归删除文件夹。
Have a look at: Directory.Delete Method (String, Boolean)[^], see the caode sample for deleting recursively a folder.
嗨
我通过在microsoft论坛中获取解决方案解决了这个问题,我发布代码用vb语言描述
Hi I solved this one by getting solution in microsoft forums,Below i post that codes in vb language with description
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
为了便于澄清你可以参考这个链接
http://social.msdn.microsoft .COM /论坛/的Silverlight / EN-US / 6eac546b-6663-4c36-aa53-44f44a15db33 /删除 - 内容 - 的 - 一个文件夹,包括-所有子文件夹,和文件?论坛= vblanguage [< a href =http://social.msdn.microsoft.com/Forums/silverlight/en-US/6eac546b-6663-4c36-aa53-44f44a15db33/deleting-contents-of-a-folder-including-all-subfolders-和文件?forum = vblanguagetarget =_ blanktitle =新窗口> ^ ]
问候
Aravind
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
这篇关于如何删除它包含文件和子文件夹的主文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!