如何删除它包含文件和子文件夹的主文件夹?

编程入门 行业动态 更新时间:2024-10-08 19:48:50
本文介绍了如何删除它包含文件和子文件夹的主文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨我尝试删除包含文件和子文件夹的主文件夹,使用此代码

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

为了便于澄清你可以参考这个链接 social.msdn.microsoft .COM /论坛/的Silverlight / EN-US / 6eac546b-6663-4c36-aa53-44f44a15db33 /删除 - 内容 - 的 - 一个文件夹,包括-所有子文件夹,和文件?论坛= vblanguage [ ^ ] 问候 Aravind

And for ur clarification u can refer this link social.msdn.microsoft/Forums/silverlight/en-US/6eac546b-6663-4c36-aa53-44f44a15db33/deleting-contents-of-a-folder-including-all-subfolders-and-files?forum=vblanguage[^] Regards Aravind

更多推荐

如何删除它包含文件和子文件夹的主文件夹?

本文发布于:2023-11-28 10:42:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642071.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主文   文件夹   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!