0

I am using below code for launch my program on startup:

RegistryKey rk = Registry.CurrentUser.OpenSubKey
    ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    rk.SetValue(AppName, Application.ExecutablePath);

but when run my project this error occourrs:

System.UnauthorizedAccessException: 'Attempted to perform an unauthorized operation.'

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
s.g
  • 17
  • 4

1 Answers1

1

There was this issue a long time ago.

first you need to set registry in HKCU\Software\Microsoft\Windows\CurrentVersion\Run make sure you have enough permissions!:

        using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
        {
            key.SetValue(AppName, Application.ExecutablePath);
        }

Then follow the steps from this post:

  1. Add it to the current user's Startup folder. This requires the least permissions for your app to run, and gives the user the most control and feedback of what's going on. The down-side is that it's a little more difficult determining whether to show the checkbox already checked next time they view that screen in your program.
  2. Add it to the HKey_Current_User\Software\Microsoft\Windows\CurrentVersion\Run registry key. The only problem here is that it requires write access to the registry, which isn't always available.
  3. Create a Scheduled Task that triggers on User Login
  4. Add it to the HKey_Local_Machine\Software\Microsoft\Windows\CurrentVersion\Run registry key. The only problem here is that it requires write access to the registry, which isn't always available.
  5. Set it up as a windows service. Only do this if you really mean it, and you know for sure you want to run this program for all users on the computer.
Barr J
  • 10,636
  • 1
  • 28
  • 46