gpt4 book ai didi

c# - 将注册表写入 hkey_current_user 而不是 hkey_users

转载 作者:行者123 更新时间:2023-11-30 12:09:05 24 4
gpt4 key购买 nike

我试着像这样将注册表子项及其对应的值写入注册表:

Microsoft.Win32.RegistryKey mykey;
mykey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\Muhil Software");
mykey.SetValue("Muhil", "TEST");
mykey.close();

如您所见,我使用CurrentUser将值写入HKEY_CURRENT_USERHKEY_CURRENT_USER中没有任何内容。然后查看HKEY_USERS中的子键,发现键写在那里。

最佳答案

您的安装程序不在登录用户 session 下运行,而是在本地系统 session 下运行。这解释了为什么 HKCU指向另一个注册表配置单元。

为了打开登录用户的注册表项,您需要打开此注册表项HKU/<LOGGED_ON_USER_SID> .得益于 Windows Session API,您可以获得此 SID(安全标识符)。

您可以使用 Microsoft.Win32.Registry.Users而不是 Microsoft.Win32.Registry.CurrentUser并根据用户 SID 打开正确的用户 key 。

您可以在 stackoverflow 上找到几个关于如何获取当前登录 SID 的主题,例如 How to get a Unique ID for the current user's logon session in windows - c#

更新:能够获取登录用户 SID 字符串的示例代码,它只能在系统 session 中工作,因为它需要特殊权限 SE_TCB_NAME .为简单起见,没有错误处理

static void Main(string[] args)
{
Microsoft.Win32.RegistryKey mykey;
mykey = Microsoft.Win32.Registry.Users.CreateSubKey(GetLoggedOnUserSID() + "\\Software\\Microsoft\\Windows\\Muhil Software");
mykey.SetValue("Muhil", "TEST");
mykey.Close();
}

enum TokenInformationClass
{
TokenOwner = 4,
}

struct TokenOwner
{
public IntPtr Owner;
}

[DllImport("advapi32.dll", EntryPoint = "GetTokenInformation", SetLastError = true)]
static extern bool GetTokenInformation(
IntPtr tokenHandle,
TokenInformationClass tokenInformationClass,
IntPtr tokenInformation,
int tokenInformationLength,
out int ReturnLength);

[DllImport("kernel32.dll")]
private static extern UInt32 WTSGetActiveConsoleSessionId();

[DllImport("wtsapi32.dll", SetLastError = true)]
static extern bool WTSQueryUserToken(UInt32 sessionId, out IntPtr Token);

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool ConvertSidToStringSid(IntPtr sid, [In, Out, MarshalAs(UnmanagedType.LPTStr)] ref string pStringSid);

static string GetLoggedOnUserSID()
{
IntPtr tokenOwnerPtr;
int tokenSize;
IntPtr hToken;

// Get a token from the logged on session
// !!! this line will only work within the SYSTEM session !!!
WTSQueryUserToken(WTSGetActiveConsoleSessionId(), out hToken);

// Get the size required to host a SID
GetTokenInformation(hToken, TokenInformationClass.TokenOwner, IntPtr.Zero, 0, out tokenSize);
tokenOwnerPtr = Marshal.AllocHGlobal(tokenSize);

// Get the SID structure within the TokenOwner class
GetTokenInformation(hToken, TokenInformationClass.TokenOwner, tokenOwnerPtr, tokenSize, out tokenSize);
TokenOwner tokenOwner = (TokenOwner)Marshal.PtrToStructure(tokenOwnerPtr, typeof(TokenOwner));

// Convert the SID into a string
string strSID = "";
ConvertSidToStringSid(tokenOwner.Owner, ref strSID);
Marshal.FreeHGlobal(tokenOwnerPtr);
return strSID;
}

关于c# - 将注册表写入 hkey_current_user 而不是 hkey_users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39261491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com