如何删除子目录
假设有一些路径: C:\Temp\TestFolder1\TestFolder2
我有一些模板: C:\Temp
所以我想编写将通过模板删除所有子目录的函数
void DeleteSubdirectories(string tlt, string path) {}
如果我使用给定参数调用此函数
DeleteSubdirectories("C:\Temp", "C:\Temp\TestFolder1\TestFolder2");
它必须从 'TestFolder1\TestFolder2 子目录中删除>C:\Temp
编写此函数的最佳方法是什么?
Lets say a have some path:
C:\Temp\TestFolder1\TestFolder2
And I have some template:
C:\Temp
So I want to write function that will delete all subdirectories by template
void DeleteSubdirectories(string tlt, string path) {}
If I call this function with given parameters
DeleteSubdirectories("C:\Temp", "C:\Temp\TestFolder1\TestFolder2");
It must delete TestFolder1\TestFolder2 subdirectories from 'C:\Temp
What is the best way to write this function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您也想删除“C:\Temp”,请使用以下命令:
如果您只想删除子目录,请使用以下命令:
If you desire to delecte "C:\Temp" too, use this:
If you just want to delete the sub directories use this:
只需使用
Directory.Delete
-我链接的重载有一个布尔值,指示是否也应删除子目录。Just use
Directory.Delete
- the overload I linked has a boolean value that indicates if subdirectories should also be deleted.你所描述的听起来很奇怪,但是试试这个:
使用它:
显然,你必须实现异常处理。
What you're describing sounds wierd, but try this:
Use it like:
Obviously, you'll have to implement the exception handling.