By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Get access denied error when using DirectoryInfo.delete

P: n/a
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,
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Charts,

Where is the executable being run from? Is it being run from a network
share, perhaps?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Charts" <Ac*****@newsgroup.nospam> wrote in message
news:04**********************************@microsof t.com...
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,

Nov 17 '05 #2

P: n/a
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?

Nov 17 '05 #3

P: n/a
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 [.NET/C# MVP]" wrote:
Charts,

Where is the executable being run from? Is it being run from a network
share, perhaps?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Charts" <Ac*****@newsgroup.nospam> wrote in message
news:04**********************************@microsof t.com...
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,


Nov 17 '05 #4

P: n/a
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.

Nov 17 '05 #5

P: n/a

"Charts" <Ac*****@newsgroup.nospam> wrote in message
news:04**********************************@microsof t.com...
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.

Nov 17 '05 #6

P: n/a
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[i].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.
""Jeffrey Tan[MSFT]"" wrote:
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.

Nov 17 '05 #7

P: n/a
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.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.