删除文件夹及其内容

时间:2015-12-10 06:40:57

标签: c# asp.net-mvc asp.net-mvc-5 delete-file

我刚跟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文件夹中的所有contnet,但它最终没有删除07文件夹。

我在这一行Directory.Delete(path1);

中收到错误

一旦我调试,我可以通过以下消息看到运行时错误

  

无法找到路径'C:\ Program Files(x86)\ IIS的一部分   快递\〜\内容\ Essential_Folder \ attachments_AR \ 07' 。

但是path1值为~/Content/Essential_Folder/attachments_AR/07

6 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

您无法通过提供其物理路径来删除目录。来自使用Directory.Delete()的网络应用程序,因此您必须使用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));

答案 2 :(得分:0)

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

Directory.Delete(attachments_AR.FullName);

答案 3 :(得分:0)

在变量中单独使用文件夹的路径,并在Directory.Delete(。)中传递此变量,如:

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

答案 4 :(得分:0)

为什么不使用Directory类(MSDN documentation)中的方法:

public static void Delete(
    string path,
    bool recursive
)

您的代码将更清晰,更简单,但更重要的是,手动构建路径时,您可以实现路径长度限制。我得到这样的问题,解决方案是使用这种方法。

答案 5 :(得分:0)

这是我所做的解决方案,也是删除根文件夹:

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();
    }