Get access denied error when using DirectoryInfo.delete

G

Guest

I login as administrator of the machine. However running the following code
get access denied exception for file inside the directory.

The source code:

DirectoryInfo target = new DirectoryInfo(TargetDirectory);
target.Delete(true);

I don’t have problem manually delete the whole directory. Please advice.
Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

Charts,

Where is the executable being run from? Is it being run from a network
share, perhaps?
 
B

Bry

Random thoughts to follow up on Nicholas' suggestion

Is the directory you are tring to delete empty?

Does your applicaiton (or any other) have a lock on the directory?
 
G

Guest

The application is a console application and run in the local machine. The
folder to be deleted is a local folder. As I mentioned that I login as
machine administrator account.
The folder is not empty and has both sub directories and files. However I
set flag to true in target.Delete(true). That suppose to delete all sub
directories and files.
Do I need to set up anything in FileIOPermission in order to use
DirectoryInfo class to delete a folder contains files? How do I relate
FileIOPermission class to DirectoryInfo class?
Thanks,
Charts

Nicholas Paldino said:
Charts,

Where is the executable being run from? Is it being run from a network
share, perhaps?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Charts said:
I login as administrator of the machine. However running the following code
get access denied exception for file inside the directory.

The source code:

DirectoryInfo target = new DirectoryInfo(TargetDirectory);
target.Delete(true);

I don't have problem manually delete the whole directory. Please advice.
Thanks,
 
J

Jeffrey Tan[MSFT]

Hi Charts,

Thanks for your feedback.

Yes, this issue is really strange, when you are lauching the application in
local disk with admin account, your application should have full trust CAS
in .Net.

Ok, let's first give a evaluation to your assembly. You can open ".Net
Configuration 1.1" tool from "Administrative tools" in Control Panel. Click
"Runtime Security Policy" node the left panel. Then click "Evaluate
Assembly" link in the right panel.

In the popup dialog, input your assembly path, and click next to evaluate
your assembly permission set. Normally, it should be Unrestricted.

I original suspected that this may caused by Windows NTFS DACL permission,
however, as you said, you can delete this directory manually without any
problem, I just assume that you did not do impersonation in this console
application and it runs under your current login accout. So the NTFS should
not be a problem....

Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
W

Willy Denoyette [MVP]

Charts said:
I login as administrator of the machine. However running the following code
get access denied exception for file inside the directory.

The source code:

DirectoryInfo target = new DirectoryInfo(TargetDirectory);
target.Delete(true);

I don’t have problem manually delete the whole directory. Please advice.
Thanks,

Some possible causes:
- The directory is open in the exploere.
- It's the CWD for your console application.
- Another program has a contained file open

Willy.
 
G

Guest

Thanks Jeffrey for the information. I followed up and checked the assembly
permission. It is unrestricted. So that setting is fine. Actually I find
out that the reason that I cannot delete folder is that some of my files in
the folder are in read only mode. After I set them to normal mode using
“File.SetAttributes(Finfo.FullName, FileAttributes.Normal);â€
I was able to delete the folder. So the case can be closed now. It is
interesting that it is no where documented that if you use DirectoryInfo
delete method the files in the folder cannot be in read only mode. Thanks for
you help.
Charts.
 
J

Jeffrey Tan[MSFT]

Hi Charts,

Oh, I am glad we finally got the root cause.
Actually, based on my experience, if we are deleting a folder with
read-only files in it, windows explorer will pop-up a warning dialog to
show the read-only information. I think this warning dialog information
should be a good hint to us: there are read-only files in the folder.

Ok, if you need further help, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top