码迷,mamicode.com
首页 > Windows程序 > 详细

C#直接删除指定目录下的所有文件及文件夹(保留目录)

时间:2015-12-20 15:54:29      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

?

????#region 直接删除指定目录下的所有文件及文件夹(保留目录)

/// <summary>

/// 直接删除指定目录下的所有文件及文件夹(保留目录)

/// </summary>

/// <param name="strPath">文件夹路径</param>

/// <returns>执行结果</returns>

public bool DeleteDir(string strPath)

{

try

{

// 清除空格

strPath = @strPath.Trim().ToString();

// 判断文件夹是否存在

if (System.IO.Directory.Exists(strPath))

{

// 获得文件夹数组

string[] strDirs = System.IO.Directory.GetDirectories(strPath);

// 获得文件数组

string[] strFiles = System.IO.Directory.GetFiles(strPath);

// 遍历所有子文件夹

foreach (string strFile in strFiles)

{

// 删除文件夹

System.IO.File.Delete(strFile);

}

// 遍历所有文件

foreach (string strdir in strDirs)

{

// 删除文件

System.IO.Directory.Delete(strdir, true);

}

}

// 成功

return true;

}

catch (Exception Exp) // 异常处理

{

// 异常信息

System.Diagnostics.Debug.Write(Exp.Message.ToString());

// 失败

return false;

}

}

#endregion

C#直接删除指定目录下的所有文件及文件夹(保留目录)

标签:

原文地址:http://www.cnblogs.com/Chary/p/5061001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!