Curly Brace
Curly Brace

Reputation: 535

How can I set this registry value for my User from my installer

The problem from https://stackoverflow.com/a/37859812/4878558

I need to set Registry value for current user, who launch the install up. Since install going for system mode - I don't know anything about current user

Also my code giving 'System.UnauthorizedAccessException'

SecurityIdentifier sID = WindowsIdentity.GetCurrent().User;
var subKey = Registry.Users.OpenSubKey(sID + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
subKey.SetValue("test", "test");
enter code here

Upvotes: 0

Views: 1841

Answers (2)

PhilDW
PhilDW

Reputation: 20780

As Ripple and I have both commented, there's no need for code. Go to the Registry view in the setup project, right-click on Software under HKEY_CURRENT_USER and add the key Microsoft, then Windows, the CurrentVersion, then Run, adding each key.

Then in the Run key view, right-click in the Name, View pane on the right and add new string value, the name being your name. The value, I assume, is the path to your exe, and (assuming it's in the Application folder) make the value [TARGETDIR]my.exe.

If your install is an "Everyone" install then there is a perfectly good reason why it cannot work. This is nothing to do with the code. In an Everyone install that custom action code is running with the System account (NOT the installing user) so you are trying to create a run key for the system account.

Upvotes: 1

vmg
vmg

Reputation: 10566

Here is how to write autostartup options:

const string AutorunRegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run";
Registry.SetValue(AutorunRegistryKey, <AppName>, <PathToApplication>);

If you want to remove it from autostartup:

const string AutorunRelativePath = @"Software\Microsoft\Windows\CurrentVersion\Run\";
var key = Registry.CurrentUser.OpenSubKey(AutorunRelativePath, true);
    if (key != null)
    {
        key.DeleteValue(<AppName>, false);
        key.Close();
    }

Upvotes: 0

Related Questions