How can I set this registry value from my installe

2020-04-20 12:10发布

In my .msi installer package, I have a C# custom action that writes a registry value in:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

The custom action is deferred because I need elevated privileges for some of the keys I am trying to install. However, because it is deferred, this action writes to the current user of the system account since it is launched with elevated permissions, so my registry value actually gets written in:

HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Run

How can I get the installer to write this registry value into the registry of the user who launched the installation package instead of the system account's registry?

1条回答
放我归山
2楼-- · 2020-04-20 12:24
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();

Do you try this, here reference from microsoft

Edit:

            string strSID = "";
            string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            NTAccount ntuser = new NTAccount(userName);
            SecurityIdentifier sID = (SecurityIdentifier)ntuser.Translate(typeof(SecurityIdentifier));
            strSID = sID.ToString();

            Registry.Users.SetValue(sID + @"\key", value);

Try this, you should probably read about Registry.Users.SetValue

You need:

using System.Security.Principal;

for this code.

查看更多
登录 后发表回答