c#中的Windows版本

我想知道PC有哪个Windows版本..在C#Framework 3.5中

我试过用

OperatingSystem os = Environment.OSVersion;

版本ver = os.Version;

但结果是

Plataform:WIN32NT

版本6.2.9200

版本未成年人:2

版本专业:6

问题是我有“Windows 8 Pro”……

我怎样才能发现它?

谢谢

您必须自己将版本号与相应的字符串值进行匹配。

以下是最新Windows操作系统及其相应版本号的列表:

  • Windows Server 2016技术预览版 – 10.0 *
  • Windows 10 – 10.0 *
  • Windows 8.1 – 6.3 *
  • Windows Server 2012 R2 – 6.3 *
  • Windows 8 – 6.2
  • Windows Server 2012 – 6.2
  • Windows 7 – 6.1
  • Windows Server 2008 R2 – 6.1
  • Windows Server 2008 – 6.0
  • Windows Vista – 6.0
  • Windows Server 2003 R2 – 5.2
  • Windows Server 2003 – 5.2
  • Windows XP 64位版 – 5.2
  • Windows XP – 5.1
  • Windows 2000 – 5.0

*对于已针对Windows 8.1或10显示的应用程序。未显示为8.1 / 10的应用程序将返回Windows 8 OS版本值(6.2)。

这是来源 。

另外,来自同一来源:

识别当前操作系统通常不是确定特定操作系统function是否存在的最佳方法。 这是因为操作系统可能在可再发行的DLL中添加了新function。 而不是使用Version API Helper函数来确定操作系统平台或版本号,而不是测试function本身的存在。

在我的场景中,我需要我的应用程序来捕获可能的错误报告和统计信息的计算机信息。

我没有找到必须添加应用程序清单的解决方案。 不幸的是,我在google搜索时发现的大多数建议都表明了这一点。

事实上,当使用清单时,必须手动添加每个操作系统版本,以便该特定操作系统版本能够在运行时报告自身。

换句话说,这成为竞争条件:我的应用程序的用户可能正在使用我的应用程序的版本,该应用程序在使用之前的操作系统之前。 当微软推出新的操作系统版本时,我必须立即升级应用程序。 我还必须强制用户在更新操作系统的同时升级应用程序。

换句话说, 不太可行

在浏览选项后,我发现了一些引用(与应用清单相比很少见),而是建议使用注册表查找。

我的(砍掉) ComputerInfo类只有WinMajorVersionWinMinorVersionIsServer属性,如下所示:

 using Microsoft.Win32; namespace Inspection { ///  /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup. ///  public static class ComputerInfo { ///  /// Returns the Windows major version number for this computer. ///  public static uint WinMajorVersion { get { dynamic major; // The 'CurrentMajorVersionNumber' string value in the CurrentVersion key is new for Windows 10, // and will most likely (hopefully) be there for some time before MS decides to change this - again... if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMajorVersionNumber", out major)) { return (uint) major; } // When the 'CurrentMajorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion' dynamic version; if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version)) return 0; var versionParts = ((string) version).Split('.'); if (versionParts.Length != 2) return 0; uint majorAsUInt; return uint.TryParse(versionParts[0], out majorAsUInt) ? majorAsUInt : 0; } } ///  /// Returns the Windows minor version number for this computer. ///  public static uint WinMinorVersion { get { dynamic minor; // The 'CurrentMinorVersionNumber' string value in the CurrentVersion key is new for Windows 10, // and will most likely (hopefully) be there for some time before MS decides to change this - again... if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentMinorVersionNumber", out minor)) { return (uint) minor; } // When the 'CurrentMinorVersionNumber' value is not present we fallback to reading the previous key used for this: 'CurrentVersion' dynamic version; if (!TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentVersion", out version)) return 0; var versionParts = ((string) version).Split('.'); if (versionParts.Length != 2) return 0; uint minorAsUInt; return uint.TryParse(versionParts[1], out minorAsUInt) ? minorAsUInt : 0; } } ///  /// Returns whether or not the current computer is a server or not. ///  public static uint IsServer { get { dynamic installationType; if (TryGeRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType", out installationType)) { return (uint) (installationType.Equals("Client") ? 0 : 1); } return 0; } } private static bool TryGeRegistryKey(string path, string key, out dynamic value) { value = null; try { var rk = Registry.LocalMachine.OpenSubKey(path); if (rk == null) return false; value = rk.GetValue(key); return value != null; } catch { return false; } } } } 

我发布了OsInfo nuget以轻松比较Windows版本。

 bool win8OrLess = Environment.OSVersion.IsLessThanOrEqualTo(OsVersion.Win8); bool winXp = Environment.OSVersion.IsEqualTo(OsVersion.WinXP); int? servicePack = Environment.OSVersion.GetServicePackVersion(); bool is64bit = Environment.OSVersion.Is64Bit(); // Already covered in .NET 4.5+ 

试试这个:

 using System.Management; private string fnGetFriendlyName() { var name = (from x in new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().OfType() select x.GetPropertyValue("Caption")).FirstOrDefault(); return name != null ? name.ToString() : "Unknown"; } 

资料来源: https : //stackoverflow.com/a/2016557/3273962