我有一个 C# 应用程序,我必须在其中删除一个目录:
if (buttonName == "Supprimer projet")
{
if (url != null)
{
proj.DeleteDirectory(proj.GetProjectsId(url));
var path = Path.Combine(@"C:\Projets", url);
try
{
Directory.Delete(path);
}
catch { }
return RedirectToAction("Gestion", "Admin");
}
}
但我有这个异常 the directory is not empty\r\n
。
这个错误的原因是什么?我该如何解决?
最佳答案
使用接受 bool 值作为第二个参数并传递 true 的重载。这将递归地删除目录中的所有文件和目录。参见 http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx
Directory.Delete 方法(字符串, bool 值):
public static void Delete(
string path,
bool recursive
)
关于c# - 使用 System.IO.Directory.Delete 删除目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16958061/