C# .NET - System.UnauthorizedAccessException

Asked By Munendra Maheshwari on 12-Jan-09 03:18 AM

I am working in c# (dot net 3.5) on Windows vista.  I am trying to create a directory at run time by using the following code

System.IO.Directory.CreateDirectory(@"C:\Program Files\Docflow");

Every time I found the error of System.UnauthorizedAccessException: Access to the path 'C:\Program Files\Docflow' is denied.
Wht should I do to prevent this.

This command works fine in Windows XP, but exception occurs in Windows Vista.

Reply

Asked By Munendra Maheshwari on 12-Jan-09 03:36 AM

This is not a proper solution for me. I want a solution by which I can create a directory at runtime in C:\\Program File\\ directory in Windows Vista.

Please give me the code line for this....

Try this

ram kumar replied to Munendra Maheshwari on 12-Jan-09 03:39 AM

Hi,

1. Give the administarator rights to that folder

2. Then u can access the folder


Rams

You do not have priveleges to the C:\Program Files folder.

[)ia6l0 iii replied to Munendra Maheshwari on 12-Jan-09 03:39 AM

This could be due to the fact , that you are not in an Administrator's role on the machine, or you have restricted priveleges.

Although the fix is down here, i would suggest you not to create directory in Program Files or any other directory that need Administrative priveleges. You would landup in some or the other security sooner or later.

You can use the User Profile Folder or the "Debug" folder itself for such things.

Solution: Right - Click on the folder and select "Properties". Go to the Security tab and ensure that the current user has sufficient rights to write.

try this
C_A P replied to Munendra Maheshwari on 12-Jan-09 04:07 AM
make sure you unset the read only attribute if it is set.

Refer to .net documentation for:
1. system.io.file
2. file.setattributes method
read this
C_A P replied to Munendra Maheshwari on 12-Jan-09 04:10 AM
Exception Details: System.UnauthorizedAccessException: Access is denied.

ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request identity.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or
Network Service on IIS 6) that is used if the application is not
impersonating. If the application is impersonating via <identity
impersonate="true"/>, the identity will be the anonymous user (typically
IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in Explorer,
choose "Properties" and select the Security tab. Click "Add" to add the
appropriate user or group. Highlight the ASP.NET account, and check the boxes
for the desired access.
try this link
C_A P replied to Munendra Maheshwari on 12-Jan-09 04:11 AM

http://www.xtremedotnettalk.com/showthread.php?t=85640

http://bytes.com/groups/net-asp/569738-system-unauthorizedaccessexception-access-denied-excel-application

http://www.velocityreviews.com/forums/t383094-systemunauthorizedaccessexception-access-is-denied-excel-application.html

 

reply
Munendra Maheshwari replied to C_A P on 12-Jan-09 05:45 AM

string a = Environment.UserName;

System.Security.Principal.NTAccount idenrity = new System.Security.Principal.NTAccount(a);

string FileName = @"C:\Program Files\";

DirectoryInfo dInfo = new DirectoryInfo(FileName);

DirectorySecurity dSecurity = dInfo.GetAccessControl();

dSecurity.AddAccessRule(new FileSystemAccessRule(idenrity.Value, FileSystemRights.FullControl, AccessControlType.Allow));

// dSecurity.AddAccessRule(new FileSystemAccessRule(a, FileSystemRights.FullControl, AccessControlType.Allow));

dInfo.SetAccessControl(dSecurity);

System.IO.Directory.CreateDirectory(@"C:\Program Files\dd\");

 

I use this code actually, but found exception. I wanna create a new directory in Program files folder in c: drive. and i work in c# in vista. can any one help me to overcome this problem.

 

re
Web Star replied to Munendra Maheshwari on 12-Jan-09 07:14 AM

The most frustrating error in ASP.NET is when you locate your Web folder outside of the wwwroot folder. If you copy file to that location, then giving the ASPNET or IUSR_XXX user access doesn't always help.  You also have to give permission to IIS_WPG. I pier developer had this problem 3 years ago and simply couldn't find this solution even after days of research. Hopefully, this will save someone somewhere some time.

Reply
Munendra Maheshwari replied to Web Star on 12-Jan-09 10:46 PM

I am working on a windows based application in c# on windows vista.

And when I try to create a directory anywhere dynamically by C# code except "C:\Program files\", it creates the directory successfully, but in case of "C:\Program Files\" it gives the error of as I described above.

Please suggest...