1

I have the following code and I am trying to delete a folder it works most of the time but occasionally I get an IOException and visual studio says "access denied" The path exist and the folder says it is empty, and no subfolders but I can not delete it using the following code. Any suggestion why this would work most of the time but not always and how to fix it

if ( Directory.Exist( dir2 + "\\Inv")
   {
    System.IO.DirectoryInfo dirinv = new System.IO.DirectoryInfo(dir2 + "\\Inv");
    setAttributesNormal(dirinv);
    try
    {
         Directory.Delete(dir2 + "\\Inv", true);
    }
    catch (IOException)
    {
        Directory.Delete(dir2 + "\\Inv", true);
    }
    catch (UnauthorizedAccessException)
    {
        Directory.Delete(dir2 + "\\Inv", true);
    }
 }
}

private void setAttributesNormal(DirectoryInfo dir)
{
     foreach (var subDir in dir.GetDirectories())
     setAttributesNormal(subDir);
     foreach (var file in dir.GetFiles())
    {
         file.Attributes = FileAttributes.Normal;
     }
}
Tate
  • 31
  • 8
  • 1
    Does this answer your question? [C# Remove all empty subdirectories](https://stackoverflow.com/questions/2811509/c-sharp-remove-all-empty-subdirectories) – Jawad Jan 31 '20 at 16:26
  • 5
    It sounds as though your application doesn't have permissions to delete it. Have you tried running it/Visual Studio in Administrator mode? – Steve Jan 31 '20 at 16:27
  • I've seen this when antivirus gets in the way, if you've just done something else to the folder (like deleting the files inside it) – canton7 Jan 31 '20 at 16:30
  • 1
    If you previously, in you application, had opened files in that folder, ensure to `Close/Dispose` these files - so all file handles are released. Else the folder can be "locked" for that reason. – Frank Nielsen Jan 31 '20 at 16:38
  • 1
    If you have a command prompt window open inside that folder, the folder can't be deleted even though its empty. It's like having permission to remove a rug, but if someone is standing on it, you'll have a hard time. –  Jan 31 '20 at 16:39
  • I tried running as administrator and that did not help, I did a get files to make sure it was truly empty and it is, and I just have code that is trying to the folder to make sure there are no open handles and I still get access denied – Tate Jan 31 '20 at 23:31

1 Answers1

0

Run the program (or if running in Visual Studio, then Visual Studio) by File Explorer Run as Administrator by right clicking on the app.

Visual Studio does not run as admin when one launches it. One must run it with elevated privlidges to do such operations on protected folders or to have access to the ports such as running IE Express from Visual Studio.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • I tried running as administrator and that did not help, I did a get files to make sure it was truly empty and it is, and I just have code that is trying to the folder to make sure there are no open handles and I still get access denied – Tate Jan 31 '20 at 19:21