kez
kez

Reputation: 2313

delete a folder and its content

I just followed this tutorial to delete a folder and its content

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

     }

But using this its deleting all the contnet in 07 folder, but its not deleting the 07 folder finally.

I'm getting error in this line Directory.Delete(path1);

Once I debug I can see run time error with below message

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

but path1 value is ~/Content/Essential_Folder/attachments_AR/07

Upvotes: 2

Views: 8403

Answers (6)

Leo
Leo

Reputation: 315

Here is a solution what I did and it is deleting the root folder too:

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

Upvotes: 0

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

The reason is that Directory.Delete cannot recognize ~ in the path.

You need to convert it to an absolute path using Server.MapPath() like you did it here:

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

You may also want to convert it once, and use in both methods:

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

    ....
} 

By the way, there is absolutely no need to remove files manually. You can use

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

    Directory.Delete(mappedPath, true);
} 

which will remove all folders, subfolders and files recursively, and then will remove directory itself.

Upvotes: 3

Klaudiusz bryjamus
Klaudiusz bryjamus

Reputation: 326

Why don't you use method from Directory class (MSDN documentation):

public static void Delete(
    string path,
    bool recursive
)

Your code will be cleaner and simplier but much more important is that when building path manually you can achive path long limit. I get issue like this and solution was use this method.

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29006

you cannot delete a directory by giving its physical path. from a web application using Directory.Delete(), so you have to convert it into absolute path by using Server.MapPath()

Use : Directory.Delete(Server.MapPath(path1));

Or you can use Like the following without using EmptyFolder() method :

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

Upvotes: 2

Vaibhav Chaurasiya
Vaibhav Chaurasiya

Reputation: 166

Just take separate path of the folder in variable and pass this variable in Directory.Delete(.) Like :

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

Upvotes: 0

user1672994
user1672994

Reputation: 10839

you should delete the directly using full path instead of relative path. Try with

Directory.Delete(attachments_AR.FullName);

Upvotes: 0

Related Questions