tags:

views:

80

answers:

5

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

A: 

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...

Sergio
Yes, I've set the Application Pool to NetworkService and My admin account both don't seem to work.
Kramer
A: 

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.

Jesse C. Slicer
Yes, I'm doing that. The <code>Impersonator.LogOn</code> is that code.
Kramer
+1  A: 

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
    }
}
DOK
That way my thoughts too. But now I get Access to the path 'index.asp' is denied.
Kramer
You're trying to delete a web page?
DOK
Yes and images why now?
Kramer
Well, deleting web pages from a website that is running could pose a problem. Perhaps if you edit your question to add that information, you can get a recommendation for that specific situation. It may be permissions, or it could be that the file is in use.
DOK
Yeah, when I ran the same thing from my desktop it work. I think it's permission but I don't know where to edit it in the app pool as someone suggested. Or if I'm impersonating correctly.
Kramer
Sometimes it isn't enough to just say impersonate="true" in web.config. Sometimes you have to actually supply a login name and password. You might try that with your own credentials, and if it works, you may need a special user account for your app.
DOK
A: 

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.

C Johnson
This is where I'm still a novice, how do I get more info?
Kramer
A: 

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.

Ryan Ternier
Yes it did work from my desktop but not from the server. So is it a permission issue on the apppool? If so where do I change it?
Kramer