1

The code

Here's a grossly simplified pseudo-code version of some C# code that I'm debugging.

// 1) Lots of files and directories are copied

// 2) Some unnecessary files are deleted

// 3) Try to delete an unnecessary directory
string stubbornFolder = @"C:\...\Stubborn"; // This folder was created during step 1 above.
Directory.Delete(stubbornFolder);

The problem

When it hits Directory.Delete, the following exception is thrown.

System.IO.IOException: Access to the path 'C:\...\Stubborn' is denied.

Notes

  • Deleting the directory "C:...\test" also works as expected. test and Stubborn both appear to have the same security settings. Both directories are completely empty.

  • If I manually delete then re-create Stubborn using Windows Explorer (and skip over the copying code using a debugger), the deletion works as expected.

    • Manual deletion works even when done while the application is running.
  • Peeking at the access control types like suggested in this question seemed to indicate that all rules were set to Allow.

  • The unnecessary file deletion works as expected.

  • Running the executable as an admin vs. not as an admin doesn't make any difference.

  • There doesn't appear to be any applications that are using Stubborn as their working directory.

Help!

Any ideas on what might be causing this?

Community
  • 1
  • 1
sgryzko
  • 2,397
  • 2
  • 22
  • 37
  • 3
    Do you have some program keeping the folder open somewhere? – Steve Dec 31 '15 at 17:00
  • How is this folder created? By the same application? – jHilscher Dec 31 '15 at 17:02
  • What if you try to use the Win32 API directly? https://msdn.microsoft.com/en-us/library/aa365488(VS.85).aspx the comment by mhubal shows a c# version – Camilo Terevinto Dec 31 '15 at 17:06
  • @Steve I don't think so. I'm allowed to move, rename, and delete the folder manually. – sgryzko Dec 31 '15 at 17:08
  • maybe you got admin privileges and the application not? Is the app open while you manually delete the folder? – jHilscher Dec 31 '15 at 17:09
  • @jHilscher yes it it created by the same application during the copy step. – sgryzko Dec 31 '15 at 17:11
  • @cFrozenDeath I'll try that. Thanks. – sgryzko Dec 31 '15 at 17:11
  • Thanks everyone for the comments. I've edited my post accordingly. – sgryzko Dec 31 '15 at 17:15
  • 2
    Is there any code prior to Directory.Delete(stubbornFolder); that references stubbornFolder (or any file within) and isn't letting go? Also, you mentioned that you can rename/move/delete the directory manually, is that after you've closed your program? And have you tried with your program running after it's already called Directory.Delete? – Tim Dec 31 '15 at 17:25
  • Adding to @Tim's questions, does any running process (including yours) have the current working directory set to the one you try to delete? – dxiv Dec 31 '15 at 22:46
  • Another answer that could help out: http://stackoverflow.com/a/648055/3543437 – kayleeFrye_onDeck Jan 20 '16 at 21:48

1 Answers1

1

There may be some sort of attribute preventing you from deleting the folder. You might try setting the attributes to normal first:

System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(@"C:\...\Stubborn");
setAttributesNormal(directory);



void setAttributesNormal(System.IO.DirectoryInfo directory){
    foreach (string subDirectoryPath in directory.GetDirectories()){
        var directoryInfo = new DirectoryInfo(subDirectoryPath);
        foreach (string filePath in directoryInfo.GetFiles()) {
            var file = new FileInfo(filePath);
            file.Attributes = FileAttributes.Normal;
        }
    }
}
Penguinparty
  • 126
  • 4
  • It doesn't look like you ever set the attributes of 'Stubborn' to normal here. Did you forget to do that or did you mean to leave it out? Also, what is 'setAttributesNormal'? I'll suggest an edit. – sgryzko Jan 04 '16 at 10:13