1

This code:

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

            rKey.DeleteValue(Application.ProductName, false);
            rKey.SetValue(Application.ProductName, Application.ExecutablePath, RegistryValueKind.String);

doesn't work on Windows 8. I don't have idea why because on Windows 7 and on Windows XP this solution works.

Can you help me?

Iain Ballard
  • 4,433
  • 34
  • 39
  • 1
    Are you running as administrator? – Silvermind Mar 18 '14 at 18:17
  • 3
    "It does not work" is pretty vague. Care to elaborate? – Rad1 Mar 18 '14 at 18:18
  • I'm running as administrator. When I lunch program, he works corretly. But after restart OS, he doesn't work automatically. I noticed this problem only with Windows 8 because after restart Windows 7 or Windows XP, program work automatically. – user3434436 Mar 18 '14 at 18:27
  • I tried HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run and HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, but this does not help. Every time, i have to manual lunch application on Windows 8. – user3434436 Mar 18 '14 at 18:33

1 Answers1

4

In order to set something in the registry you need to run the application as an administrator. To do so you first add a Application Manifest File to the Properties "folder" in the project.

Then you change

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

To:

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

Then I don't know if the way you get the current executable path is correct, for me this have worked at least:

class Program
{
    private static void RegisterAsRun()
    {
        string exePath = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath;           
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "TestApp", exePath, RegistryValueKind.String);
    }


    static void Main(string[] args)
    {
        RegisterAsRun();

        Console.WriteLine("Hello!");
        Console.ReadLine();
    }
}

Another note is that if the application is compiled in x86 and the OS is x64 the registry key will end up in the Wow64 registry which makes it the following path:

HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run

Robin Andersson
  • 5,150
  • 3
  • 25
  • 44