我想在 C# .NET 代码中获取底层机器操作系统信息及其其他详细信息。
在 Windows 10 专业版机器上运行以下代码时,它返回错误的值作为“Windows 10 企业版”。
Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "").ToString();
在注册表上还有其他要查询的键吗?否则它是通过其次要和主要版本细节手动解释的?
答案 0 :(得分:0)
它需要根据您的系统在 32 位或 64 位注册表中明确查询。
此解决方案不使用任何技巧来获取操作系统版本。它提供与导航到路径时在注册表中看到的完全相同的值。
例如。就我而言,它返回“Windows 10 Pro”
RegistryKey localKey = null;
if (Environment.Is64BitOperatingSystem) {
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry64);
} else {
localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
RegistryView.Registry32);
}
var productName = localKey.OpenSubKey(@ "SOFTWARE\Microsoft\Windows
NT\CurrentVersion").GetValue("ProductName").ToString();
Console.WriteLine("ProductName : " + productName);