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

2023-12-06

In my .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();

你试试这个吗,这里参考微软

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

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

你需要:

using System.Security.Principal;

对于这段代码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从安装程序中设置此注册表值 的相关文章

随机推荐