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. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...
随机推荐
- Android之捕获TextView超链接
应该是好久没有写有关技术类的文章了,今天分享一篇捕获TextView超链接的文章,希望对大家有所帮助,我终于在歪路上回归正途了.这个捕获TextView超链接应该算是比较常用吧,如果你会了,就不用看了 ...
- 基于DDS的任意波形发生器
实验原理 DDS的原理 DDS(Direct Digital Frequency Synthesizer)直接数字频率合成器,也可叫DDFS. DDS是从相位的概念直接合成所需波形的一种频率合成技术. ...
- 【对noip结束后一个月内的总结】
最近在刷一些树结构,但发现没有一个提纲,觉得有点不知所措,经常学完一个就发现还有比它更好的,而且比较耗时间.于是沙茶准备按顺序刷bzoj的省选题,看看效果怎么样……求大神指教
- 通过Keepalived实现Redis Failover自动故障切换功能
通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keep ...
- 高手详解SQL性能优化十条经验
1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...
- JPA Hibernate应用实例
1.配置文件:persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persiste ...
- 【Tyvj 1060】【NOIP 2005】等价表达式
设a为一个质数,模数为另一个质数,然后暴力算多项式的答案,如果答案相等就认为两个多项式相等. 这种hash有出错概率的题为什么还是要用hash呢?因为出错的概率实在太小了,a和模数的值取得好出题人根本 ...
- 状态压缩 HDU4539 郑厂长系列故事——排兵布阵
多组n *m 0不能放1可以放 每个士兵可以攻击到并且只能攻击到与之曼哈顿距离为2的位置以及士兵本身所在的位置. #include<stdio.h> #include<algorit ...
- lucene-查询query->QueryParser
对于搜索引擎(比如Google和百度)来讲,很多情况下只需要用户在输入框内输入所需查询的内容,然后再单击“搜索”就可以了,其余的事情全部交给搜索引擎去处理,最后搜索引擎会把检索到的结果显示出来.那么搜 ...
- mima开发实列
最顶层父基类Clinet:用于记录公共内容 切供多个Clinet继承公用 import java.net.InetSocketAddress; import java.nio.charset.Char ...