问题描述
我可以使用 C# 从注册表中读取 Windows“产品名称”
Registry.GetValue ( @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion","ProductName","" )
我运行的是 64 位版本的 Windows Pro,但从 32 位应用程序返回“Windows 10 Enterprise”。
事实上,如果我查看注册表,我可以在密钥中看到这一点
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
值为“Windows 10 Pro”,但在密钥中
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion
它是“Windows 10 企业版”。
这有意义吗?
解决方法
只要您不想支持 Windows Vista 之前的操作系统版本,就可以使用 GetProductInfo。
,我不知道为什么 32 位和 64 位配置单元不同。在我全新安装的 Windows 10 Pro x64 21H1 中,32 位版本的 Edition
为 "Enterprise"
,ProductName
为 "Windows 10 Enterprise"
。 64 位注册表具有正确的 "Pro"
值。
我的 32 位进程默认使用 x64 操作系统上的 32 位注册表。如果代码确实在 32 位操作系统上运行,我想介绍这种情况。
它不是单行的,但我用这段代码根据操作系统选择了注册表。
var hklm64 = RegistryKey.OpenBaseKey(
RegistryHive.LocalMachine,Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32
);
RegistryKey rk = hklm64.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
string product = (string)rk.GetValue("ProductName");
string edition = (string)rk.GetValue("Edition");