System.IO.Directory.Delete目录删除
在程序运行的时候,如果直接获取一个目录路径,然后执行删除(包括子目录及文件):
System.IO.Directory.Delete(path,true);
或者
System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(path);
downloadedMessageInfo.Delete(true);
如果手动在目录里面复制一个文件然后再粘贴一个副本相当于添加文件或者目录(而不是删除)就会报错:
{System.IO.IOException: 目录不是空的。
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
at System.IO.DirectoryInfo.Delete(Boolean recursive)
这样的错误只有在svn的目录里面才能出现,不知道为什么。
System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(dirPath);
foreach (FileInfo file in downloadedMessageInfo.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
{
dir.Delete(true);
}
也可以使用DirectoryInfo 扩展方法:
public static class DirectoryExtensions
{ public static void Empty(this System.IO.DirectoryInfo directory)
{
foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}
}
System.IO.Directory.Delete目录删除的更多相关文章
- System.IO.Directory类
1.参考的博客:System.IO.Directory类和System.DirectoryInfo类(http://blog.sina.com.cn/s/blog_614f473101017du4.h ...
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...
- System.IO.Directory.cs
ylbtech-System.IO.Directory.cs 1.返回顶部 1. #region 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, Pub ...
- 在c#中过滤通过System.IO.Directory.GetDirectories 方法获取的是所有的子目录和文件中的系统隐藏的文件(夹)的方法
//读取目录 下的所有非隐藏文件夹或文件 public List<FileItem> GetList(string path) { int i; string[] folders = Di ...
- 详解C#中System.IO.File类和System.IO.FileInfo类的用法
System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...
- System.IO
I/O 1.文件操作:File (1)void AppendAllText(string path, string contents) (2)bool Exist ...
- System.IO命名空间下常用的类
System.IO System.IO.Directory 目录 System.IO.Path 文件路径(包含目录和文件名) System.IO.FileInfo 提供创建.复制.删除.移动和打开文件 ...
- System.IO.File类和System.IO.FileInfo类
1.System.IO.File类 ※文件create, copy,move,SetAttributes,open,exists ※由于File.Create方法默认向所有用户授予对新文件的完全读写. ...
- java File delete 无法删除文件的原因。
windows下使用java.io.File.delete()方法删除文件时,返回值为true. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...
随机推荐
- Google搜索的几个使用技巧——让你的搜索结果更准确
对于软件开发人员来说,不知道的内容在网上搜索是再正常不过的了.今天同事在组内分享了几个谷歌搜索的使用技巧,在此自己总结一下,希望可以帮到更多人. 在此之前先要唠叨几句,什么时候用百度,什么时候用谷歌? ...
- centos 7 下进入单用户模式修改root密码
centos7进入单用户模式修改root用户密码 在工作中可能会遇到root密码忘记,那么这里就要进入单用户模式下了. 在重启电脑之后 时间:2015-05-13 10:42来源:blog.51cto ...
- Swift开发小技巧--扫描二维码,二维码的描边与锁定,设置扫描范围,二维码的生成(高清,无码,你懂得!)
二维码的扫描,二维码的锁定与描边,二维码的扫描范围,二维码的生成(高清,无码,你懂得!),识别相册中的二维码 扫描二维码用到的三个重要对象的关系,如图: 1.懒加载各种类 // MARK: - 懒加载 ...
- Linux_rsylogd日志轮替(三)
一.轮替规则及配置文件:vi /etc/logrotate.conf 1.如果配置文件中拥有" dateext"参数,那么日志会用日期来作为日志文件的后缀,例如" sec ...
- 使用HTML5新特性Mutation Observer实现编辑器的撤销和撤销回退操作
MutationObserver介绍 MutationObserver给开发者们提供了一种能在某个范围内的DOM树发生变化时作出适当反应的能力.该API设计用来替换掉在DOM3事件规范中引入的Mut ...
- Spring不支持依赖注入static静态变量
在springframework里,我们不能@Autowired一个静态变量,使之成为一个spring bean,例如下面这样: 可以试一下,yourClass在这种状态下不能够被依赖注入,会抛出运行 ...
- 100722A
这道题抄了答案: 思路:旋转,其实只用旋转四次,因为在换行的过程中旋转其实是没有意义的,因为行列只不过转了个角度.然后主要的是行列的交换,这里我很头疼,写了个盲目搜索,当然wa掉了 问了问某位同志,是 ...
- JS-slider.js实现鼠标拖动滑块控制取值特效
制作效果,如下图,鼠标点击颜色标能左右拖动并设置文本框中的值 源码: <div id="example"> <div id="slideContaine ...
- Linux命令:修改文件权限命令chmod、chgrp、chown的区别
chmod是更改文件的权限 chown是改改文件的属主与属组 chgrp只是更改文件的属组. (1)chmod是修改文件/目录的权限.可以有文字修改和数字修改. #chmod 777 /home/be ...
- 自定义不等高cell—storyBoard或xib自定义不等高cell
1.iOS8之后利用storyBoard或者xib自定义不等高cell: 对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView(cell的content ...