0

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

  • Here is the error message, turns out it isn't access denied. https://i.gyazo.com/thumb/1200/_53ca184381424e0a67aceec700838eae-png.jpg – Special KKK Apr 14 '17 at 03:10
  • 3
    Gee, if only there was a way to get some "Details" about the exception. – jmcilhinney Apr 14 '17 at 03:43
  • You should just have maybe a proper path: `HKEY_LOCAL_MACHINE` or `HKEY_CURRENT_USER` should be in front – Mederic Apr 14 '17 at 06:48
  • @Mederic : Notice how he's using `LocalMachine.OpenSubKey`. – Visual Vincent Apr 14 '17 at 09:19
  • @VisualVincent Yeah I know but maybe it doesn't like it. I remembered in past I had problems with subkeys would work with some ways and not with others if the application wasn't launched with admin privileges – Mederic Apr 14 '17 at 09:22
  • @Mederic : He requires admin privileges, yes (I just posted an answer about it), but that was not what you were saying before. `LocalMachine` will always get the `HKLM` key, so it shouldn't be included in the path. – Visual Vincent Apr 14 '17 at 09:35
  • @VisualVincent Yeah i know but so that he doesn't need admin priv I was thinking maybe can get rid the `LocalMachine` and just use the path directly sometimes it used to bypass on vista don't know if still does . BTW can you check the recent vb.net post about HWID the guy says not working but pretty sure it works. – Mederic Apr 14 '17 at 09:38
  • @Mederic : No, you can't bypass it. You are required to have admin privileges or Windows won't let you write to `HKLM`. -- As for the latest question; I don't have Windows XP nor 8 so I can't test it. – Visual Vincent Apr 14 '17 at 09:40
  • 1
    @VisualVincent Ok thanks for clearing it up ;) – Mederic Apr 14 '17 at 09:41
  • I would look at details but as I said, it only appears on my friend's machine and he is too lazy... – Special KKK Apr 15 '17 at 04:04
  • Then why are you asking us for help? Without looking at the details you will never find out where that NRE was thrown. We don't have close to enough code to identify anything. -- I don't even think the line that you show us throws the error (something is VERY wrong if it does). – Visual Vincent Apr 15 '17 at 04:37

1 Answers1

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" />
    
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75