c# - 使用 System.IO.Directory.Delete 删除目录

标签 c# .net file io directory

我有一个 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/

相关文章:

C# - MVVM 中的第二个选项卡项中不显示弹出窗口

c# - Windows 商店应用程序中的密码散列

c# - 数据未显示在数据 GridView 中

java - 扫描仪帮助 - Java

斯卡拉喷雾。将文本输出为文件

c# - 是否可以在 RMQ .NET 中指定多个连接点?

c# - 按路径加载 XML 文件

c# - XNA C#SoundEffectInstance-没有声音

c# - Code Cop 应用程序挂起

php - .php 与 .html - 为什么不总是使用 .php?