delete a folder and its content

kez

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

Yeldar Kurmangaliyev

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delete a folder and its content AWS S3 java

From Dev

How to delete all the folder except one folder and its content using command prompt?

From Dev

Find and delete folder but not content

From Dev

Java - Delete folder and its contents

From Dev

Route an entire folder and its content

From Dev

Is it possible to delete all content of a folder?

From Dev

How to delete a user & its home folder safely?

From Dev

How to move folder and its content in powershell

From Dev

How to move folder and its content in powershell

From Dev

delete directory object and its content in powershell

From Dev

Delete files from a folder with changing content

From Dev

Cannot delete folder - Content seems to be nested recursively

From Dev

Delete the parent folder keeping all content

From Dev

PHP Delete all folder content without deleting root folder

From Dev

Find and delete all files without extensions witin a folder and its subfolders

From Dev

Delete all files from a folder and its sub folders

From Dev

Find and delete all files without extensions witin a folder and its subfolders

From Dev

Delete files older than 7 days in folder and its subfolder

From Dev

batch move/cut folders and its content to another folder

From Dev

In Android, would it be possible to open a file in the 'values' folder and to read its content?

From Dev

Make innosetup copy the full folder instead of only its content

From Dev

How to check if a folder is empty or not and use its content in an if-then-else-statement?

From Dev

How to find and disable a content control by tag to delete it and its contents?

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How to delete everything inside a directory, without an specific folder and it's content

From Dev

7-Zip: Automate script with excluding folder but include its content(set folder as root itself) in .cbr structure

From Dev

How to copy a folder by overwriting an existing folder and delete all the old content in Linux?

From Dev

How to delete an optional content group alongwith its content from pdf using pdfbox?

Related Related

  1. 1

    Delete a folder and its content AWS S3 java

  2. 2

    How to delete all the folder except one folder and its content using command prompt?

  3. 3

    Find and delete folder but not content

  4. 4

    Java - Delete folder and its contents

  5. 5

    Route an entire folder and its content

  6. 6

    Is it possible to delete all content of a folder?

  7. 7

    How to delete a user & its home folder safely?

  8. 8

    How to move folder and its content in powershell

  9. 9

    How to move folder and its content in powershell

  10. 10

    delete directory object and its content in powershell

  11. 11

    Delete files from a folder with changing content

  12. 12

    Cannot delete folder - Content seems to be nested recursively

  13. 13

    Delete the parent folder keeping all content

  14. 14

    PHP Delete all folder content without deleting root folder

  15. 15

    Find and delete all files without extensions witin a folder and its subfolders

  16. 16

    Delete all files from a folder and its sub folders

  17. 17

    Find and delete all files without extensions witin a folder and its subfolders

  18. 18

    Delete files older than 7 days in folder and its subfolder

  19. 19

    batch move/cut folders and its content to another folder

  20. 20

    In Android, would it be possible to open a file in the 'values' folder and to read its content?

  21. 21

    Make innosetup copy the full folder instead of only its content

  22. 22

    How to check if a folder is empty or not and use its content in an if-then-else-statement?

  23. 23

    How to find and disable a content control by tag to delete it and its contents?

  24. 24

    How can I delete the "sheet" node keeping its content intact?

  25. 25

    How can I delete the "sheet" node keeping its content intact?

  26. 26

    How to delete everything inside a directory, without an specific folder and it's content

  27. 27

    7-Zip: Automate script with excluding folder but include its content(set folder as root itself) in .cbr structure

  28. 28

    How to copy a folder by overwriting an existing folder and delete all the old content in Linux?

  29. 29

    How to delete an optional content group alongwith its content from pdf using pdfbox?

HotTag

Archive