如何从安装程序中设置此注册表值

本文关键字:设置 注册表 程序 安装 | 更新日期: 2023-09-27 18:25:47

在我的.msi安装程序包中,我有一个C#自定义操作,它在中写入注册表值

HKEY_CURRENT_USER'Software'Microsoft'Windows'CurrentVersion'Run

自定义操作被推迟,因为我需要提升我试图安装的某些密钥的权限。然而,由于它是延迟的,这个操作会写入系统帐户的当前用户,因为它是用提升的权限启动的,所以我的注册表值实际上会写入:

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

如何让安装程序将此注册表值写入启动安装包的用户的注册表,而不是系统帐户的注册表?

如何从安装程序中设置此注册表值

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();

你尝试过这个吗,这里参考来自微软

编辑:

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);

试试这个,你可能应该读一下Registry.Users.SetValue

您需要:

using System.Security.Principal;

用于此代码。