问题
I added the Manifest file with the element
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
I built the project and it keeps crashing when a non-administrator runs it.
Unhandled Exception: System.Security.SecurityException: Request for the permissi on of type 'System.Security.Permissions.RegistryPermission, mscorlib, Version=2. 0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMa rk& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at Microsoft.Win32.RegistryKey.CheckSubKeyReadPermission(String subkeyName) at Microsoft.Win32.RegistryKey.CheckOpenSubKeyPermission(String subkeyName, B oolean subKeyWritable) at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at LotusTrustedSites.RegistryKeyExtensionMethods.GetOrCreateSubKey(RegistryKe y registryKey, String parentKeyLocation, String key, Boolean writable) at LotusTrustedSites.ReportDownloader.Main(String[] args) The action that failed was: Demand The type of the first permission that failed was: System.Security.Permissions.RegistryPermission
The assembly or AppDomain that failed was: MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null The method that caused the failure was: Microsoft.Win32.RegistryKey GetOrCreateSubKey(Microsoft.Win32.RegistryKey, Syste m.String, System.String, Boolean)
回答1:
Of course it fails. You told it to require administrator access, and non-administrator accounts don't have the ability to provide that. Did you expect it to run with administrator privileges for non-admin users? That would be a major security flaw in Windows if it were possible.
If you need an app to run as an administrator that is accessible to non-admins, the only way I know to do it is to install the app as a scheduled task, and set the task to run using an admin account. Non-admins should still be able to see and run this task, and it will use the supplied admin credentials.
回答2:
You specified requireAdministrator
, but you're not running it as an administrator. You should either change it to asInvoker
or you should run it as an administrator.
回答3:
As far as I can see, you're trying to obtain some registry permissions. This is the reason it needs administrative privileges.
Try to revisit (or post) the code that deals with registry access and try to figure out what causes the problem
来源:https://stackoverflow.com/questions/6059615/net-console-app-keeps-crashing