/// <summary>
/// 递归文件夹 删除文件夹下面的所有文件及目录
/// </summary>
/// <returns></returns>
public ContentResult RecursionFolder()
{
string path = Server.MapPath("/Content/Recursion");
if (System.IO.Directory.Exists(path))
{
//方法一
//DeleteDirFile(path);
//方法二
DeleteDirectoryAndFile(path);
}
return Content("success");
}
private void DeleteDirectoryAndFile(string path)
{
string[] fileDirs=Directory.GetFileSystemEntries(path);
foreach (var item in fileDirs)
{
//文件
if (System.IO.File.Exists(item))
{
System.IO.File.Delete(item);
}
//目录
if (Directory.Exists(item))
{
DeleteDirectoryAndFile(item);
//删除空目录
Directory.Delete(item);
}
}
}