Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


C#
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));
   dInfo.SetAccessControl(dSecurity);
}



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


Exception Details are:


System.UnauthorizedAccessException was unhandled
Message="Attempted to perform an unauthorized operation."
Source="mscorlib"
StackTrace:
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
Updated 17-Sep-12 20:27pm
v5
Comments
pradeep rasara 18-Sep-12 4:32am    
please help me to resolve this problem.

1 solution

According to the official documentation: http://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception.aspx[^]:

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[^]
 
Share this answer
 
v3
Comments
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
fjdiewornncalwe 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.
fjdiewornncalwe 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900