Unhandled Exception when trying to add registry key

混江龙づ霸主 提交于 2019-12-31 05:21:09

问题


When I use the following code My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)

to add a registry key to make my program startup automatically, it works on my system but on everyone who I have sent it to gets an "Unhandled Exception" message saying that access is denied. I was wondering if there is a solution to this. Thanks


回答1:


You get "Access denied" because you are required to run your app with administrative privileges if you want to write to the HKEY_LOCAL_MACHINE key.

You can force your app to only run with, and therefore always ask for, admin privileges by doing the following steps:

  1. Right-click your project in Visual Studio's Solution Explorer window and press Properties.

  2. Make sure you are on the Application tab, then press the button that says View Windows Settings.

  3. In the app.manifest file that opens in the editor locate the following:

    <requestedExecutionLevel level="asInvoker" uiAccess="false" />
    

    and replace it with:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    


来源:https://stackoverflow.com/questions/43404463/unhandled-exception-when-trying-to-add-registry-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!