Kramer
Kramer

Reputation: 148

Deleting a directory

I'm having some major trouble deleting directories. I'm working building a ADMIN tool to delete directories that my clients create but then ask to delete them. You would think this is simple:


using (var context = Impersonator.LogOn(user, password, domain))
{
    try
    {
        dir = new DirectoryInfo(path);
        dir.Delete(true);
    }
    catch (Exception ex)
    {
        return string.Format("Error:{0}", ex.Message);
    }
    finally
    {
        context.Undo();
    }
}

Now no matter what I do I can't delete the folder. response is alway "Access to the path is denied". I've doubled check the path, the login everything.

Please tell me what I'm doing wrong.

Server: win2008 web edition ASP.NET: 4

Upvotes: 1

Views: 319

Answers (5)

Ryan Ternier
Ryan Ternier

Reputation: 8804

Forgive me if you have done this but:

1) Can you test the method you're using to delete a directory using unit tests? Ensure that the delete code works when ran locally on your machine using tests.

2) The reason you might be getting denied is because the files are in use. If that index.asp file in one of the previous posts was ever started by IIS, it could still be in use and thus not wanting to be deleted. IF IIS has it's hands on your files, then you'll need to stop the IIS service or wait until it is released.

3) Are the files read-only?

4) Ensure you're using physical paths, not relative paths that only IIS can understand. Deleting files/directories will not understand virtual directories.

ON another note... why not have references to these files in a database, and do a soft-delete? Seeing it's web, users will not be able to tell. Let the DB tell the application what files you're allowed to see. This way there is low potential for data loss.

Upvotes: 0

C.J.
C.J.

Reputation: 16081

You know, I would get rid of catching the generic SystemException and instead catch some of the specific exceptions that the Delete method would throw. It might help in debugging problems like this.

Upvotes: 0

DOK
DOK

Reputation: 32841

I recently solved this identical problem by first deleting all the files in the folder, then deleting the folder.

To me, the error message about access was misleading.

Sometimes I also encounter this when deleting files in Windows Explorer. Once in a while, it balks at deleting the directory until you delete the files in it. I have never figured out why.

Here's my code:

private static void FileCleanup(string directoryName)
{
    try
    {
        string[] filenames = Directory.GetFiles(directoryName);

        foreach (string filename in filenames)
        {
            File.Delete(filename);
        }

        if (Directory.Exists(directoryName))
        {
            Directory.Delete(directoryName);
        }
    }
    catch (Exception ex)
    {
       // you might want to log it, or swallow any exceptions here
    }
}

Upvotes: 1

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

Perhaps the code examples here will point you in the right direction. You need to be running under the impersonation context to be able to do things as that user.

Upvotes: 0

Sergio
Sergio

Reputation: 8259

The error message says it all. "Access to the path is denied" --> Probably the process you are running does not have permissions to the folder

edit: Or some files are being used...

Upvotes: 0

Related Questions