Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directory.Delete doesn't work. Access denied error but under Windows Explorer it's ok

Tags:

I have searched the SO but find nothing.

Why this doesn't work?

Directory.Delete(@"E:\3\{90120000-001A-0000-0000-0000000FF1CE}-C"); 

Above line will throw exception "Access is denied". I have admin rigths and I can delete the dir with Explorer.

It looks like some forbidden chars? but Windows Explorer can handle it. How can I delete directories with names like that?

like image 246
binball Avatar asked Nov 09 '09 14:11

binball


People also ask

When I try to delete a folder it says access denied?

To work around this issue, use either of the following methods: When you delete the files or folders by using Windows Explorer, use the SHIFT+DELETE key combination. This bypasses the Recycle Bin. Open a command prompt window and then use the rd /s /q command to delete the files or folders.

How do I delete a Windows 10 Access Denied folder?

Select the “Security” tab. Select the “Advanced” button. Select the “Continue” button. Under the “Permissions” tab, select the line that is set to “Deny Everyone” permission, then select “Remove“.

How do I fix command prompt access denied?

Start a Command Prompt as Administrator by right-clicking on the "Command Prompt" icon in the Windows Start Menu and choose "Run as administrator". Click Continue if you are presented with a confirmation popup message box. In the new command prompt, enter "net user administrator /active:yes".


1 Answers

Thank you all for your input, it helps me in quick find of solution.

As Phil mentioned "Directory.Delete fails if it is, regardless of permissions (see bottom of msdn.microsoft.com/en-us/library/…)"

In addition Unable to remove Read-Only attribute from folder Microsoft says:

You may be unable to remove the Read-Only attribute from a folder using Windows Explorer. In addition, some programs may display error messages when you try to save files to the folder.

Conclusion: always remove all dir,file attributes diffrent then Normal before deleting. So below code solve the problem:

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"E:\3\{90120000-0021-0000-0000-0000000FF1CE}-C1");  if (dir.Exists) {     setAttributesNormal(dir);     dir.Delete(true); }  . . .  function setAttributesNormal(DirectoryInfo dir) {     foreach (var subDir in dir.GetDirectories())         setAttributesNormal(subDir);     foreach (var file in dir.GetFiles())     {         file.Attributes = FileAttributes.Normal;     } } 
like image 112
binball Avatar answered Oct 13 '22 13:10

binball