我正在尝试在Windows 10计算机上使用C#获取Windows版本。
我总是得到这些值(用C#\ C ++):
专业:6
轻微:2
这是Windows 8操作系统, 相应的MSDN
C#代码:
var major = OperatingSystem.Version.Major var minor = OperatingSystem.Version.Minor
C ++代码
void print_os_info() { //http://stackoverflow.com/questions/1963992/check-windows-version OSVERSIONINFOW info; ZeroMemory(&info, sizeof(OSVERSIONINFOW)); info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW); LPOSVERSIONINFOW lp_info = &info; GetVersionEx(lp_info); printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion); }
Windows 10假设与那些:
less校:10
次要:0 *
build立者:10.0.10586.0(th2_release.151029-1700)
我在这里错过了什么?
在我的情况下,我需要我的应用程序捕获可能的错误报告和统计信息的计算机信息。
我没有找到应用程序清单必须满意的解决方案。 大部分我在google上发现的建议都表明,不幸的是,
事情是,在使用清单时,必须手动添加每个操作系统版本,以便特定操作系统版本能够在运行时自行报告。
换句话说,这成为一个竞争条件:我的应用程序的用户可能很好地使用我的应用程序版本,在使用的操作系统的日期 。 当微软启动新的操作系统版本时,我将不得不立即升级应用程序。 我还必须强制用户在更新操作系统的同时升级应用程序。
换句话说, 不是很可行。
浏览完这些选项之后,我发现了一些引用(与应用程序清单相比令人惊讶的少),而不是建议使用注册表查找。
只有WinMajorVersion
, WinMinorVersion
和Isserver
属性的我的(切碎) ComputerInfo
类看起来像这样:
using Microsoft.Win32; namespace Inspection { /// <summary> /// Static class that adds convenient methods for getting information on the running computers basic hardware and os setup. /// </summary> public static class ComputerInfo { /// <summary> /// Returns the Windows major version number for this computer. /// </summary> 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 (TryGetRegistryKey(@"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 (!TryGetRegistryKey(@"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; } } /// <summary> /// Returns the Windows minor version number for this computer. /// </summary> 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 (TryGetRegistryKey(@"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 (!TryGetRegistryKey(@"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; } } /// <summary> /// Returns whether or not the current computer is a server or not. /// </summary> public static uint Isserver { get { dynamic installationType; if (TryGetRegistryKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "InstallationType", out installationType)) { return (uint) (installationType.Equals("Client") ? 0 : 1); } return 0; } } private static bool TryGetRegistryKey(string path, string key, out dynamic value) { value = null; try { using(var rk = Registry.LocalMachine.OpenSubKey(path)) { if (rk == null) return false; value = rk.GetValue(key); return value != null; } } catch { return false; } } } }
你需要添加一个app.manifest
到你的应用程序中:
然后取消注释以下行:
<!-- Windows 10 --> <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
您可以通过代码从regsirty中读取,并执行您想要的具体操作。
例如说:
注册表项可以在这里找到:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion,然后查找“ProductName”
您可以通过运行regedit.exe打开注册表信息(Windows + r)
var reg =Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion"); string productName = (string)reg.GetValue("ProductName"); if (productName.StartsWith("Windows 10")) { } else { }