使用System.IO.Directory.Delete删除目录

Lam*_*fif 1 .net c# directory io file

我有ac#应用程序,我必须删除一个目录:

 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");
                }
            }
Run Code Online (Sandbox Code Playgroud)

但我有这个例外the directory is not empty \r\n.

这个错误的原因是什么?我该如何解决?

cre*_*mor 5

使用接受布尔值的重载作为第二个参数并传递true.这将递归删除目录中的所有文件和目录.请参阅http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

Directory.Delete方法(String,Boolean):

public static void Delete(
    string path,
    bool recursive
)
Run Code Online (Sandbox Code Playgroud)