0

I have a requirement for an application to "run on start-up" for all users. This has to be setup by the application it's self, not by a user adding a shortcut to start-up folder or something similar. I have managed to get this to work by adding the .exe path to the registry Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run. The following code successfully adds the path to the registry when in debug mode and visual studios is run as administrator.

using Microsoft.Win32;

var path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
try
{
    string baseDir = "EXAMPLE_DIR";
    using (RegistryKey? key = Registry.LocalMachine.OpenSubKey(path, true))
    {
        if (key is not null)
        {
            string exePath = $@"{baseDir}\iOK2.exe";
            key.SetValue("iOK2", exePath);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

However the same code fails to add to the registry when it has been "Published" to an an executable (.exe) file. Admin published executable

The code doesn't produce any errors, it seemingly executes and exists successfully. I have tried running as admin, creating an app.manifest and requiring admin, ensured it's compiled to 64-bit and is accessing the correct registry Any ideas on what is happening or something I'm missing? If this fails I could potentially call a PowerShell script to edit the registry, but would rather keep dependencies to a minimum if at all possible

User1
  • 57
  • 5
  • 1
    You could use [ProcMon](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) to see the registry accesses – Klaus Gütter Jun 16 '22 at 13:17
  • 1
    32 or 64 bit? There are separate registries for them https://learn.microsoft.com/en-us/troubleshoot/windows-client/deployment/view-system-registry-with-64-bit-windows – Charlieface Jun 16 '22 at 14:15
  • @Charlieface Just discovered when run in Visual Studio (x86) nothing happens (same as when published to exe) however when on AnyCPU it works fine. SOFTWARE\Microsoft\Windows\CurrentVersion\Run is the 64-bit registry though isn't it? – User1 Jun 16 '22 at 14:32
  • @Charlieface Just checked, you are correct they are being put into HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node...\Run registry, I thought that was 32-bit registry though, anyway issue solved I guess. Thanks – User1 Jun 16 '22 at 14:35
  • Does this answer your question? [Avoid Registry Wow6432Node Redirection](https://stackoverflow.com/questions/11808462/avoid-registry-wow6432node-redirection) – Charlieface Jun 16 '22 at 15:53

0 Answers0