c# - 我怎样才能在 C# 中获得真正的操作系统版本?

标签 c# .net

自 .NET 4 和 Windows 8 或更高平台以来,获取运行程序的操作系统版本非常不确定。

例如,在 Windows 10 中运行,如果我的项目有一个 Windows 8 DLL,则 Environment.OSVersion.Version 返回 6.2.9200.0,这是 Windows 8 并且不是 Windows 10。

此问题(crono 的回答)中解释了此行为:Windows version in c#

所以我的问题如下:我们如何才能确定(并保持 .NET 方式以便能够在非 Windows 平台上使用它)我们的应用程序运行的操作系统版本?

MSDN 链接:

https://msdn.microsoft.com/en-us/library/system.environment.osversion(v=vs.110).aspx

最佳答案

如果使用 Windows 以外的其他平台不是问题,您可以使用 Win32_OperatingSystem WMI 类。

Win32_OperatingSystem represents a Windows-based operating system installed on a computer

//using System.Linq;
//using System.Management;
var query = "SELECT * FROM Win32_OperatingSystem";
var searcher = new ManagementObjectSearcher(query);
var info = searcher.Get().Cast<ManagementObject>().FirstOrDefault();
var caption = info.Properties["Caption"].Value.ToString();
var version = info.Properties["Version"].Value.ToString();
var spMajorVersion = info.Properties["ServicePackMajorVersion"].Value.ToString();
var spMinorVersion = info.Properties["ServicePackMinorVersion"].Value.ToString();

记得添加对 System.Management.dll 的引用。

注意:

关于c# - 我怎样才能在 C# 中获得真正的操作系统版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32415679/

相关文章:

c# - Settings.Designer 文件和静态

c# - byte + byte = int...为什么?

c# - 资源中添加图片到Base64编码的ImageStream(resx)

c# - 在数据表 TVP 中传递系统名

c# - 有没有更简单的方法将多个 if/else 语句相互写入?

.Net Core/DNX 和 WebApi 压缩

c# - Windows Mobile 6.5 与 Windows Embedded Handheld 6.5 - 有何区别?

c# - 来自过滤器代码的音频点击/弹出

.net - 从托管代码执行CPU/GPU指令

javascript - 如何在Powershell中将实时输出保存到txt文件?监控脚本