C# 删除文件夹及其内容

C# 删除文件夹及其内容,c#,asp.net-mvc,asp.net-mvc-5,delete-file,C#,Asp.net Mvc,Asp.net Mvc 5,Delete File,我只是想删除一个文件夹及其内容 public ActionResult Product_Delete() { string idnumber = "07"; string path1 = @"~/Content/Essential_Folder/attachments_AR/" + idnumber; DirectoryInfo attachments_AR = new DirectoryInfo(Server.MapPath(

我只是想删除一个文件夹及其内容

    public ActionResult Product_Delete()
    {
        string idnumber = "07";

        string path1 = @"~/Content/Essential_Folder/attachments_AR/" + idnumber;

        DirectoryInfo attachments_AR = new DirectoryInfo(Server.MapPath(path1));
        EmptyFolder(attachments_AR);
        Directory.Delete(path1);

        ....
    } 

    private void EmptyFolder(DirectoryInfo directory)
    {

        foreach (FileInfo file in directory.GetFiles())
        {
            file.Delete();
        }

        foreach (DirectoryInfo subdirectory in directory.GetDirectories())
        {
            EmptyFolder(subdirectory);
            subdirectory.Delete();
        }

     }
但使用此选项可以删除
07
文件夹中的所有内容,但不会最终删除
07
文件夹

我在这行
目录中出错。删除(路径1)

调试后,我可以看到运行时错误和下面的消息

找不到路径“C:\Program Files(x86)\IIS”的一部分 Express\~\Content\Essential\u文件夹\attachments\u AR\07'


但是path1的值是
~/Content/Essential\u Folder/attachments\u AR/07

您不能通过给出目录的物理路径来删除目录。使用
Directory.Delete()
从web应用程序中删除,因此必须使用
Server.MapPath()

使用:
Directory.Delete(Server.MapPath(path1))

或者,您可以像下面这样使用,而无需使用
EmptyFolder()
方法:

DirectoryInfo dir = new DirectoryInfo(Server.MapPath(path1)); 
dir.GetFiles("*", SearchOption.AllDirectories).ToList().ForEach(file=>file.Delete()); 
// will delete all files in the folder and its sub folder
//so you don't need to iterate each sub folder and files in it
Directory.Delete(Server.MapPath(path1));

原因是
目录.Delete
无法识别路径中的
~

您需要使用
Server.MapPath()
将其转换为绝对路径,就像您在这里所做的那样:

DirectoryInfo attachments_AR = new DirectoryInfo(Server.MapPath(path1));
您可能还希望转换它一次,并在两种方法中使用:

public ActionResult Product_Delete()
{
    string idnumber = "07";

    string mappedPath1 = Server.MapPath(@"~/Content/Essential_Folder/attachments_AR/" + idnumber);

    DirectoryInfo attachments_AR = new DirectoryInfo(mappedPath1));
    EmptyFolder(attachments_AR);
    Directory.Delete(mappedPath1);

    ....
} 
顺便说一句,完全不需要手动删除文件。你可以用

public ActionResult Product_Delete()
{
    string idnumber = "07";
    string mappedPath = Server.MapPath(@"~/Content/Essential_Folder/attachments_AR/" + idnumber);

    Directory.Delete(mappedPath, true);
} 

它将递归删除所有文件夹、子文件夹和文件,然后删除目录本身。

您应该使用完整路径而不是相对路径直接删除。试一试

Directory.Delete(attachments_AR.FullName);

只需在变量中获取文件夹的单独路径,并在目录中传递此变量。删除(.)如下所示:


为什么不使用目录类()中的方法:


您的代码将更干净、更简单,但更重要的是,当手动构建路径时,您可以达到路径长度限制。我遇到这样的问题,解决方法是使用这种方法。

以下是我所做的解决方法,它也在删除根文件夹:

public ActionResult Product_Delete()
    {
        string idnumber = "07";

        string path1 = @"~/Content/Essential_Folder/attachments_AR/" + idnumber;

        DirectoryInfo attachments_AR = new DirectoryInfo(Server.MapPath(path1));
        EmptyFolder(attachments_AR);

        if (attachments_AR.Exists && IsDirectoryEmpty(attachments_AR.FullName))
            attachments_AR.Delete();


    }

    private static void EmptyFolder(DirectoryInfo directory)
    {
        foreach (FileInfo file in directory.GetFiles())
        {
            file.Delete();
        }

        foreach (DirectoryInfo subdirectory in directory.GetDirectories())
        {
            EmptyFolder(subdirectory);
            subdirectory.Delete();
        }
    }

    public static bool IsDirectoryEmpty(string path)
    {
        return !Directory.EnumerateFileSystemEntries(path).Any();
    }

除了答案-
目录。删除(附件\u AR.全名)谢谢这个答案也是正确的,但是@Yeldar首先回答说没关系,不;我已经更新了我的答案。请看一看是的,它可以优化我的代码,我只是在寻找类似的东西,谢谢
public static void Delete(
    string path,
    bool recursive
)
public ActionResult Product_Delete()
    {
        string idnumber = "07";

        string path1 = @"~/Content/Essential_Folder/attachments_AR/" + idnumber;

        DirectoryInfo attachments_AR = new DirectoryInfo(Server.MapPath(path1));
        EmptyFolder(attachments_AR);

        if (attachments_AR.Exists && IsDirectoryEmpty(attachments_AR.FullName))
            attachments_AR.Delete();


    }

    private static void EmptyFolder(DirectoryInfo directory)
    {
        foreach (FileInfo file in directory.GetFiles())
        {
            file.Delete();
        }

        foreach (DirectoryInfo subdirectory in directory.GetDirectories())
        {
            EmptyFolder(subdirectory);
            subdirectory.Delete();
        }
    }

    public static bool IsDirectoryEmpty(string path)
    {
        return !Directory.EnumerateFileSystemEntries(path).Any();
    }