Click here to Skip to main content
13,295,192 members (68,588 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am using below code to deny Access. but it generate <code>"
System.UnauthorizedAccessException: Attempted to perform an unauthorized operation.". </code>
i am running application as Run as Administrator.please help me.

string path=@"C:\Windows\System32\calc.exe;
AddDirectorySecurity(path, "everyone", FileSystemRights.ReadData, AccessControlType.Allow)

public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
   DirectoryInfo dInfo = new DirectoryInfo(FileName);
   DirectorySecurity dSecurity = dInfo.GetAccessControl();
   dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));

Exception ocour at this line " dInfo.SetAccessControl(dSecurity);"

Exception Details are:

System.UnauthorizedAccessException was unhandled
Message="Attempted to perform an unauthorized operation."
at System.Security.AccessControl.Win32.SetSecurityInfo(ResourceType type, String name, SafeHandle handle, SecurityInfos securityInformation, SecurityIdentifier owner, SecurityIdentifier group, GenericAcl sacl, GenericAcl dacl)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, SafeHandle handle, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections, Object exceptionContext)
at System.Security.AccessControl.NativeObjectSecurity.Persist(String name, AccessControlSections includeSections)
at System.Security.AccessControl.FileSystemSecurity.Persist(String fullPath)
at System.IO.Directory.SetAccessControl(String path, DirectorySecurity directorySecurity)
at System.IO.DirectoryInfo.SetAccessControl(DirectorySecurity directorySecurity)
at usb_block_demo.Form1.AddDirectorySecurity(String FileName, String Account, FileSystemRights Rights, AccessControlType ControlType) in C:\Users\Seema\Desktop\usb block demo\usb block demo\Form1.cs:line 135
at usb_block_demo.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\Seema\Desktop\usb block demo\usb block demo\Form1.cs:line 104
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at usb_block_demo.Program.Main() in C:\Users\Seema\Desktop\usb block demo\usb block demo\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Posted 17-Sep-12 3:40am
Updated 17-Sep-12 21:27pm
pradeep rasara 18-Sep-12 4:32am
please help me to resolve this problem.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

According to the official documentation:[^]:

The reasons for the exception can be found in the details of the exception object.<br />
UnauthorizedAccessException uses the HRESULT COR_E_UNAUTHORIZEDACCESS, which has the value 0x80070005.

I would suggest that you need to look at the details and/or the inner exception that you are receiving.
I'm guessing that even as a local administrator you may not have permissions that allow you to perform the action you are attempting.

Edit: Please don't repost the same question a second time. Stick to updating your original question with more information so that the thread remains intact. (Original Question[^]
pradeep rasara 17-Sep-12 9:35am
hello sir how to resolve this problem.i running this code as run as admnistrator.inner Exception is null. please suggest me how to resolve this problem
Marcus Kramer 17-Sep-12 10:07am
Did you read my answer? Did you look at the exception details and the inner exception? What did they say? We can't help you solve something if you don't provide us with appropriate information. Please use the "Improve Question" widget at the bottom of your original question and add the information we need to help you.
pradeep rasara 19-Sep-12 1:56am
yes sir i have read your answer. and look at the exception. i have paste the stack trace.
the exception message is:"Attempted to perform an unauthorized operation."
and inner Exception is NULL . how to resolve this . please suggest me
pradeep rasara 19-Sep-12 5:46am
hello sir i do not understand ,how to resolve this.please explain me clear.
Marcus Kramer 19-Sep-12 8:59am
I just looked at your code again and noticed something. The file on which you are trying to change these permissions is in a location (system32) which protects these files so you won't be able to achieve this on this directory. Try it on a file in another unprotected directory and you'll find that it will work, but it won't work on the file you are trying to affect.
pradeep rasara 19-Sep-12 9:26am
hello sir this code work if change the owner from wizard, how can we set owner to file with code.i am using this code to set owner. but not work.while when i set owner through wizard i.e security tab . then this code work fine. Any way to set owner.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171207.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100