删除文件夹及其内容

delete a folder and its content

我刚刚按照 this tutorial 删除了一个文件夹及其内容

    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 文件夹。

我在这一行中遇到错误 Directory.Delete(path1);

调试后,我可以看到 运行 时间错误并显示以下消息

Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\Content\Essential_Folder\attachments_AR'.

但路径 1 的值为 ~/Content/Essential_Folder/attachments_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));

原因是Directory.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);

只需在变量中获取文件夹的单独路径,然后在 Directory.Delete(.) 中传递此变量,例如:

var path = Server.MapPath(@"~/Test");
DirectoryInfo attachments_AR = new DirectoryInfo(path);
Directory.Delete(path);

为什么不使用目录 class (MSDN documentation):

中的方法
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();
    }