0

I have code that looks for empty directories that are older than 1 hour and deletes them

foreach (DirectoryInfo __dir in _directories)
{
    if (!__dir.EnumerateFiles().Any() && __dir.LastWriteTime < DateTime.Now.AddHours(-1))
    {
        Directory.Delete(__dir.FullName)
    }
}

That throws an exception saying access to the directory path is denied. However I'm able to manually delete the same directory through file explorer.
I tried what other people suggested (setting attributes to normal), but that didn't work.

I tried FileAttributes.Normal; and ~FileAttributes.ReadOnly; Neither of them work.
I also tried setting access control to full control of the directory I'm trying to delete as well as it's parent directories.

IsaacBok
  • 424
  • 5
  • 18

4 Answers4

1

If program posted above cannot delete the Directory but otherwise you can manually and it's a permission issue, since manually you are logged is as admin, but application is not running under administrator permission:

  • For a console / UI application, Visual studio / exe is running under lesser permission, open visual studio under admin permission or run the exe as an administrator, it shall be able to able to achieve the purpose
  • For ASP.Net web application, it would be running under the context of a IIS user, check out users and groups, provide permission to that user over the directory.
Mrinal Kamboj
  • 11,300
  • 5
  • 40
  • 74
0

If FileAttributes.Normal; doesn't do the trick (which it should) try setting the Attributes to ReadOnly:

foreach (DirectoryInfo __dir in _directories)
{
    if (!__dir.EnumerateFiles().Any() && __dir.LastWriteTime < DateTime.Now.AddHours(-1))
    {
       __dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
       __dir.Delete();
    }
}

Also note this uses __dir to delete itself instead of the Directory.Delete(__dir.FullName), it shouldn't make a difference, just shorthand.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • @IsaacBok ok, run ProcessMonitor when you try to delete the folder and show us the specific FileMon log entries. My next guess would be the folders are not empty and there's some sort of lock on a file or your delete operation is multithreaded but both of those hypothesis's don't align to your description. – Jeremy Thompson May 18 '16 at 07:42
  • That's not a processmonitor trace, [edit] your question and show a screenshot like [this](http://stackoverflow.com/a/10159778/495455) – Jeremy Thompson May 19 '16 at 23:58
  • Ok thanks for that, so McAfee folder eh? I reckon my hypthosis of locking is smack bang on target. Use [Handle](https://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx) or [Process Explorer](https://technet.microsoft.com/en-gb/sysinternals/processexplorer) to see what the handle is preventing the folder delete operation. Also, just checking, you have run through the solutions in this [similar QA](http://stackoverflow.com/questions/1701457/directory-delete-doesnt-work-access-denied-error-but-under-windows-explorer-it) yeah? – Jeremy Thompson May 20 '16 at 06:09
  • It did. One of which is you knowing how to capture a ProcMon trace. Not in the mood for attitude... – Jeremy Thompson May 20 '16 at 18:48
0

Along with the other suggestions, make sure your program isn't "doing something" in any if the directories you're trying to delete (i.e.: reading or writing to files within the directory)...

Frank Alvaro
  • 468
  • 4
  • 12
0

Similar issues wasted a lot of my time when trying to delete files and folders or copying files. In my case, the problem was caused by McAfee anti-virus on the server holding files open for scan. It is especially problematic if you alter or write a new file and then very quickly want to delete it or copy it. Can I suggest temporarily excluding the folder / files in question in your AV settings to determine if it is the culprit. If it is, then you're going to need to write some code that traps the error and re-tries until the lock on the file is removed.

Fawlty
  • 451
  • 2
  • 9