问题
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:
Right-click your project in Visual Studio's
Solution Explorer
window and pressProperties
.Make sure you are on the
Application
tab, then press the button that saysView Windows Settings
.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