C# 以编程方式获取Windows OS版本
我正试图在我的Windows 10计算机上获取带有C#的Windows版本 我总是得到这些值(使用C#\C++): 专业:6 小调:2 也就是Windows 8操作系统 C#代码:C# 以编程方式获取Windows OS版本,c#,windows,operating-system,version,windowsversion,C#,Windows,Operating System,Version,Windowsversion,我正试图在我的Windows 10计算机上获取带有C#的Windows版本 我总是得到这些值(使用C#\C++): 专业:6 小调:2 也就是Windows 8操作系统 C#代码: var major = OperatingSystem.Version.Major var minor = OperatingSystem.Version.Minor C++代码 void print_os_info() { //http://stackoverflow.com/questions/196
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应该与以下各项一起使用:
专业:10
小调:0*
- (当我从正在运行的进程中获取转储文件时,我可以看到该文件的OS版本设置为10.0)
我这里缺少什么?您需要向应用程序中添加一个
应用程序清单
:
然后取消对以下行的注释:
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
在我的场景中,我需要我的应用程序捕获计算机信息,以获取可能的错误报告和统计信息 我没有发现需要添加应用程序清单的解决方案令人满意。不幸的是,我在谷歌搜索时发现的大多数建议都表明了这一点 问题是,在使用清单时,必须手动将每个OS版本添加到清单中,以便特定OS版本能够在运行时报告自身 换句话说,这就变成了一种竞争条件:我的应用程序的用户很可能正在使用我的应用程序的一个版本,该版本比正在使用的操作系统早。当微软推出新的操作系统版本时,我必须立即升级应用程序。我还必须强制用户在更新操作系统的同时升级应用程序 换句话说,不太可行。 在浏览了这些选项之后,我发现了一些建议使用注册表查找的引用(与应用程序清单相比,这些引用少得出奇) 仅具有
WinMajorVersion
、WinMinorVersion
和IsServer
属性的My(已截断)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;
}
}
}
}
使用Microsoft.Win32;
名称空间检查
{
///
///静态类,该类添加了方便的方法,用于获取有关正在运行的计算机的基本硬件和操作系统设置的信息。
///
公共静态类计算机信息
{
///
///返回此计算机的Windows主版本号。
///
公共静态uint WinMajorVersion
{
得到
{
动态专业;
//CurrentVersion密钥中的“CurrentMajorVersionNumber”字符串值对于Windows 10是新的,
//而且很可能(希望)在MS决定改变这一点之前会有一段时间——再次。。。
if(TryGetRegistryKey(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentMajorVersionNumber”,out major))
{
返回(uint)主要;
}
//当“CurrentMajorVersionNumber”值不存在时,我们回退到读取用于此的上一个键:“CurrentVersion”
动态版本;
如果(!TryGetRegistryKey(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentVersion”,out version))
返回0;
var versionParts=((字符串)版本).Split('.');
如果(versionParts.Length!=2)返回0;
主要目的地;
返回uint.TryParse(versionParts[0],out majorAsUInt)?majorAsUInt:0;
}
}
///
///返回此计算机的Windows次要版本号。
///
公共静态uint WinMinorVersion
{
得到
{
动态小调;
//CurrentVersion密钥中的“CurrentMinorVersionNumber”字符串值对于Windows 10是新的,
//而且很可能(希望)在MS决定改变这一点之前会有一段时间——再次。。。
如果(TryGetRegistryKey)(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentMinorVersionNumber”,
外(小调)
{
返回(uint)未成年人;
}
//当“CurrentMinorVersionNumber”值不存在时,我们回过头来读取用于此操作的上一个键:“CurrentVersion”
动态版本;
如果(!TryGetRegistryKey(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“CurrentVersion”,out version))
返回0;
var versionParts=((字符串)版本).Split('.');
如果(versionParts.Length!=2)返回0;
小混血儿;
返回uint.TryParse(versionParts[1],out minorAsUInt)?minorAsUInt:0;
}
}
///
///返回当前计算机是否为服务器。
///
公共静态uint IServer
{
得到
{
动态安装类型;
如果(TryGetRegistryKey)(@“SOFTWARE\Microsoft\Windows NT\CurrentVersion”,“InstallationType”,
外安装类型)
{
返回(uint)(installationType.Equals(“客户端”)?0:1);
}
返回0;
}
}
私有静态bool TryGetRegistryKey(字符串路径、字符串键、输出动态值)
{
值=空;
尝试
{
使用(var rk=Registry.LocalMachine.OpenSubKey(path))
{
如果(rk==null)返回false;
value=rk.GetValue(键);
返回值!=null;
}
}
抓住
{
返回false;
}
}
}
}
您可以通过代码从注册表中读取数据,并执行您想要的特定操作
比如说:
注册表项位于HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\W
var reg =Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion");
string productName = (string)reg.GetValue("ProductName");
if (productName.StartsWith("Windows 10"))
{
}
else
{
}
Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuildNumber", string.Empty).ToString()
BOOL GetTrueWindowsVersion(OSVERSIONINFOEX* pOSversion)
{
// Function pointer to driver function
NTSTATUS (WINAPI *pRtlGetVersion)(
PRTL_OSVERSIONINFOW lpVersionInformation) = NULL;
// load the System-DLL
HINSTANCE hNTdllDll = LoadLibrary("ntdll.dll");
// successfully loaded?
if (hNTdllDll != NULL)
{
// get the function pointer to RtlGetVersion
pRtlGetVersion = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW))
GetProcAddress (hNTdllDll, "RtlGetVersion");
// if successfull then read the function
if (pRtlGetVersion != NULL)
pRtlGetVersion((PRTL_OSVERSIONINFOW)pOSversion);
// free the library
FreeLibrary(hNTdllDll);
} // if (hNTdllDll != NULL)
// if function failed, use fallback to old version
if (pRtlGetVersion == NULL)
GetVersionEx((OSVERSIONINFO*)pOSversion);
// always true ...
return (TRUE);
} // GetTrueWindowsVersion
[SecurityCritical]
[DllImport("ntdll.dll", SetLastError = true)]
internal static extern bool RtlGetVersion(ref OSVERSIONINFOEX versionInfo);
[StructLayout(LayoutKind.Sequential)]
internal struct OSVERSIONINFOEX
{
// The OSVersionInfoSize field must be set to Marshal.SizeOf(typeof(OSVERSIONINFOEX))
internal int OSVersionInfoSize;
internal int MajorVersion;
internal int MinorVersion;
internal int BuildNumber;
internal int PlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
internal string CSDVersion;
internal ushort ServicePackMajor;
internal ushort ServicePackMinor;
internal short SuiteMask;
internal byte ProductType;
internal byte Reserved;
}
var osVersionInfo = new OSVERSIONINFOEX { OSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)) };
if (!RtlGetVersion(ref osVersionInfo))
{
// TODO: Error handling, call GetVersionEx, etc.
}