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. 但是本地文件仍然存在,也就是说没有删除成功. 这时候你要检查下你传进来的文件目录格式是否正确. 正确: ...
随机推荐
- opencv8-GPU之相似性计算
Opencv支持GPU计算,并且包含成一个gpu类用来方便调用,所以不需要去加上什么__global__什么的很方便,不过同时这个类还是有不足的,待opencv小组的更新和完善. 这里先介绍在之前的& ...
- 拦截PHP各种异常和错误,发生致命错误时进行报警,万事防患于未然
在日常开发中,大多数人的做法是在开发环境时开启调试模式,在产品环境关闭调试模式.在开发的时候可以查看各种错误.异常,但是在线上就把错误显示的关闭. 上面的情形看似很科学,有人解释为这样很安全,别人看不 ...
- 为什么VC经常输出烫烫烫烫烫烫烫烫
为什么VC经常输出烫烫烫烫烫烫烫烫 2012-05-07 11:52 by Rollen Holt, 12747 阅读, 4 评论, 收藏, 编辑 在Debug 模式下, VC 会把未初始化的栈内存全 ...
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
- SpringMVC学习--springmvc原理
简介 springmvc是spring框架的一个模块,springmvc和spring无需通过中间整合层进行整合.springmvc是一个基于mvc的web框架. spring的结构图: mvc在b/ ...
- A+B
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...
- 【CodeVS 3160】最长公共子串
http://codevs.cn/problem/3160/ 看了好久的后缀自动机_(:з」∠)_ 对A串建立SAM,用B串去匹配A串SAM,如果在当前节点走不下去,就跳到当前节点的parent(类似 ...
- css 小三角
span{ display: inline-block; border-top: 100px solid red; border-right: 100px solid transparent; bor ...
- bzoj1051
就是一个tarjan #include<iostream> #include<stack> #include<cstdio> using namespace std ...
- MySQL 配置优化
1. 连接请求的变量: A.max_connections 如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量, 当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL ...