Windows 8.1, Win10之后,通过GetVersion and GetVersionEx 方法获取WIndows操作系统版本号的功能需要添加manifest文件后才能查找到,不然的话会查找错误,比如win10系统返给一个win8系统等。

具体的做法就是在项目文件里添加一个<***.exe>.manifest文件,文件的命名规则必须是exe文件的全名+.manifest.

这里直接上C#的代码了,C++代码相似,将在另外一篇文章里讲述:http://www.cnblogs.com/zhengshuangliang/p/5258504.html

微软技术文档参考网址:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724834(v=vs.85).aspx

/*
* Copyright (c) 2008, TopCoder, Inc. All rights reserved.
*/ using System;
using System.Runtime.InteropServices;
using System.IO; namespace TopCoder.Installation.Util.Prerequisite.Impl
{
/// <summary>
/// <para>
/// This utility is used by <see cref="PrerequisiteSoftwareValidation"/> to detect the Microsoft Windows
/// version currently running on the machine.
/// </para>
/// <para>
/// This class uses Win API function GetVersionEx() from kernel32 library to retrieve additional parameters
/// about the installed Windows version.
/// </para>
/// </summary>
///
/// <threadsafety>
/// <para>
/// This class is immutable and thread safe.
/// </para>
/// </threadsafety>
///
/// <author>saarixx</author>
/// <author>TCSDEVELOPER</author>
/// <version>1.0</version>
/// <copyright>Copyright (c) 2008, TopCoder, Inc. All rights reserved.</copyright>
public static class WindowsVersionDetector
{
/// <summary>
/// <para>
/// Represents Windows 95.
/// </para>
/// </summary>
private const string Windows95 = "Windows 95"; /// <summary>
/// <para>
/// Represents Windows 98.
/// </para>
/// </summary>
private const string Windows98 = "Windows 98"; /// <summary>
/// <para>
/// Represents Windows ME.
/// </para>
/// </summary>
private const string WindowsME = "Windows ME"; /// <summary>
/// <para>
/// Represents Windows Vista.
/// </para>
/// </summary>
private const string WindowsVista = "Windows Vista"; /// <summary>
/// <para>
/// Represents Windows Server 2008.
/// </para>
/// </summary>
private const string WindowsServer2008 = "Windows Server 2008";
private const string WindowsServer2008R2 = "Windows Server 2008 R2"; /// <summary>
/// Represents windows 10, windows server 2016 technical preview
/// </summary>
private const string Windows10 = "Windows 10";
private const string WindowsServer2016TP = "Windows Server 2016 Technical Preview"; #region Represents Windows8,8.1,server 2012,server 2012 R2 #CJF 11/06/2013
/// <summary>
/// Represents windows 8,8.1
/// </summary>
private const string Windows8 = "Windows 8";
private const string Windows8_1 = "Windows 8.1"; /// <summary>
/// Represents windows server 2012
/// </summary>
private const string WindowsServer2012 = "Windows Server 2012";
private const string WindowsServer2012R2 = "Windows Server 2012 R2";
#endregion /// <summary>
/// <para>
/// Represents Windows 7.
/// </para>
/// </summary>
private const string Windows7 = "Windows 7"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Storage.
/// </para>
/// </summary>
private const string WindowsServer2003Storage = "Windows Server 2003, Storage"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Compute Cluster Edition.
/// </para>
/// </summary>
private const string WindowsServer2003ComputeClusterEdition = "Windows Server 2003, Compute Cluster Edition"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Datacenter Edition.
/// </para>
/// </summary>
private const string WindowsServer2003DatacenterEdition = "Windows Server 2003, Datacenter Edition"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Enterprise Edition.
/// </para>
/// </summary>
private const string WindowsServer2003EnterpriseEdition = "Windows Server 2003, Enterprise Edition"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Web Edition.
/// </para>
/// </summary>
private const string WindowsServer2003WebEdition = "Windows Server 2003, Web Edition"; /// <summary>
/// <para>
/// Represents Windows Server 2003, Standard Edition.
/// </para>
/// </summary>
private const string WindowsServer2003StandardEdition = "Windows Server 2003, Standard Edition"; /// <summary>
/// <para>
/// Represents Windows XP Home Edition.
/// </para>
/// </summary>
private const string WindowsXPHomeEdition = "Windows XP Home Edition"; /// <summary>
/// <para>
/// Represents Windows XP Professional.
/// </para>
/// </summary>
private const string WindowsXPProfessional = "Windows XP Professional"; /// <summary>
/// <para>
/// Represents Windows 2000 Professional.
/// </para>
/// </summary>
private const string Windows2000Professional = "Windows 2000 Professional"; /// <summary>
/// <para>
/// Represents Windows 2000 Datacenter Server.
/// </para>
/// </summary>
private const string Windows2000DatacenterServer = "Windows 2000 Datacenter Server"; /// <summary>
/// <para>
/// Represents Windows 2000 Advanced Server.
/// </para>
/// </summary>
private const string Windows2000AdvancedServer = "Windows 2000 Advanced Server"; /// <summary>
/// <para>
/// Represents Windows 2000 Server.
/// </para>
/// </summary>
private const string Windows2000Server = "Windows 2000 Server"; /// <summary>
/// <para>
/// Represents Windows NT 4.0.
/// </para>
/// </summary>
private const string WindowsNT40 = "Windows NT 4.0"; /// <summary>
/// <para>
/// Represents Windows CE.
/// </para>
/// </summary>
private const string WindowsCE = "Windows CE"; /// <summary>
/// <para>
/// Represents Unknown.
/// </para>
/// </summary>
private const string Unknown = "Unknown"; /// <summary>
/// <para>
/// Represents VER_NT_WORKSTATION from kernel32 library.
/// </para>
/// </summary>
private const byte VER_NT_WORKSTATION = 0x01; /// <summary>
/// <para>
/// Represents VER_SUITE_ENTERPRISE from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_ENTERPRISE = 0x0002; /// <summary>
/// <para>
/// Represents VER_SUITE_DATACENTER from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_DATACENTER = 0x0080; /// <summary>
/// <para>
/// Represents VER_SUITE_PERSONAL from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_PERSONAL = 0x0200; /// <summary>
/// <para>
/// Represents VER_SUITE_BLADE from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_BLADE = 0x0400; /// <summary>
/// <para>
/// Represents VER_SUITE_STORAGE_SERVER from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_STORAGE_SERVER = 0x2000; /// <summary>
/// <para>
/// Represents VER_SUITE_COMPUTE_SERVER from kernel32 library.
/// </para>
/// </summary>
private const short VER_SUITE_COMPUTE_SERVER = 0x4000; /// <summary>
/// <para>
/// Contains operating system version information. The information includes major and minor version
/// numbers, a build number, a platform identifier, and information about product suites and the
/// latest Service Pack installed on the system.
/// </para>
/// </summary>
///
/// <remarks>
/// <para>
/// Please refer to WIN API document for more detailed information.
/// </para>
/// </remarks>
///
/// <author>saarixx</author>
/// <author>TCSDEVELOPER</author>
/// <version>1.0</version>
/// <copyright>Copyright (c) 2008, TopCoder, Inc. All rights reserved.</copyright>
[StructLayout(LayoutKind.Sequential)]
private struct OSVersionInfoEx
{
/// <summary>
/// <para>
/// The size of this data structure, in bytes.
/// </para>
/// </summary>
public int dwOSVersionInfoSize; /// <summary>
/// <para>
/// The major version number of the operating system.
/// </para>
/// </summary>
public int dwMajorVersion; /// <summary>
/// <para>
/// The minor version number of the operating system.
/// </para>
/// </summary>
public int dwMinorVersion; /// <summary>
/// <para>
/// The build number of the operating system.
/// </para>
/// </summary>
public int dwBuildNumber; /// <summary>
/// <para>
/// The operating system platform.
/// </para>
/// </summary>
public int dwPlatformId; /// <summary>
/// <para>
/// A null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack
/// installed on the system. If no Service Pack has been installed, the string is empty.
/// </para>
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
public string szCSDVersion; /// <summary>
/// <para>
/// The major version number of the latest Service Pack installed on the system. For example,
/// for Service Pack 3, the major version number is 3. If no Service Pack has been installed, the
/// value is zero.
/// </para>
/// </summary>
public short wServicePackMajor; /// <summary>
/// <para>
/// The minor version number of the latest Service Pack installed on the system. For example,
/// for Service Pack 3, the minor version number is 0.
/// </para>
/// </summary>
public short wServicePackMinor; /// <summary>
/// <para>
/// A bit mask that identifies the product suites available on the system.
/// </para>
/// </summary>
public short wSuiteMask; /// <summary>
/// <para>
/// Any additional information about the system.
/// </para>
/// </summary>
public byte wProductType; /// <summary>
/// <para>
/// Reserved for future use.
/// </para>
/// </summary>
public byte wReserved;
} /// <summary>
/// <para>
/// Retrieves information about the current operating system.
/// </para>
/// </summary>
///
/// <remarks>
/// <para>
/// Please refer to WIN API document for more detailed information.
/// </para>
/// </remarks>
///
/// <param name="osVersionInfoEx">
/// A structure that receives the operating system information.
/// </param>
/// <returns>
/// If the function succeeds, the return value is a nonzero value. If the function fails, the
/// return value is zero.
/// </returns>
[DllImport("kernel32.Dll")]
private static extern short GetVersionEx(ref OSVersionInfoEx osVersionInfoEx); /// <summary>
/// <para>
/// Retrieves the Windows version name.
/// </para>
/// <para>
/// Currently the result can be one of:
/// <list type="bullet">
/// <item>Windows 95</item>
/// <item>Windows 98</item>
/// <item>Windows ME</item>
/// <item>Windows Vista</item>
/// <item>Windows Server 2008</item>
/// <item>Windows 7</item>
/// <item>Windows Server 2003, Storage</item>
/// <item>Windows Server 2003, Compute Cluster Edition</item>
/// <item>Windows Server 2003, Datacenter Edition</item>
/// <item>Windows Server 2003, Enterprise Edition</item>
/// <item>Windows Server 2003, Web Edition</item>
/// <item>Windows Server 2003, Standard Edition</item>
/// <item>Windows XP Home Edition</item>
/// <item>Windows XP Professional</item>
/// <item>Windows 2000 Professional</item>
/// <item>Windows 2000 Datacenter Server</item>
/// <item>Windows 2000 Advanced Server</item>
/// <item>Windows 2000 Server</item>
/// <item>Windows NT 4.0</item>
/// <item>Windows CE</item>
/// <item>Unknown</item>
/// </list>
/// </para>
/// </summary>
///
/// <returns>
/// The Windows version name (e.g. "Windows Vista"), can not be null or empty.
/// </returns>
///
/// <exception cref="PrerequisiteSoftwareValidationException">
/// If an error occurred while getting the version.
/// </exception>
public static string GetVersion()
{
try
{
// Get platform
PlatformID platform = Environment.OSVersion.Platform; // Get Windows major version
int majorVersion = Environment.OSVersion.Version.Major; // Get Windows minor version
int minorVersion = Environment.OSVersion.Version.Minor; // Get OS Version Info
OSVersionInfoEx os = new OSVersionInfoEx(); if (platform == PlatformID.Win32NT)
{
os.dwOSVersionInfoSize = Marshal.SizeOf(os); // Call GetVersionEx
if (GetVersionEx(ref os) == )
{
throw new PrerequisiteSoftwareValidationException("GetVersionEx API failed.");
}
} // Default is Unknown
string versionName = Unknown; // Handle Win95, 98, ME
if ((platform == PlatformID.Win32Windows) && (majorVersion == ))
{
switch (minorVersion)
{
case :
versionName = Windows95;
break;
case :
versionName = Windows98;
break;
case :
versionName = WindowsME;
break;
}
}
else if (platform == PlatformID.Win32NT)
{
switch (majorVersion)
{
//Handle Windows 10,
//add below support Win10 info to IPSClient.exe->IPS.manifest
//http://www.itdadao.com/article/227158/
//https://msdn.microsoft.com/en-us/library/dn481241(v=vs.85).aspx
//<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
case :
if (minorVersion == )
{
if (os.wProductType == VER_NT_WORKSTATION)
{
versionName = Windows10;
}
else
{
versionName = WindowsServer2016TP;
}
}
break; // Handle Vista, Win2008,Windows 7,Windows 8/8.1, windows server 2012/2012R2
//https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx case :
#region Handle Windows8, 8.1, server 2012, server 2012 R2 #CJF 11/06/2013
if (minorVersion == )
{
if (os.wProductType == VER_NT_WORKSTATION)
{
versionName = Windows8;
}
else
{
versionName = WindowsServer2012;
}
} if (minorVersion == )
{
if (os.wProductType == VER_NT_WORKSTATION)
{
versionName = Windows8_1;
}
else
{
versionName = WindowsServer2012R2;
}
}
#endregion if (minorVersion == )
{ // Windows 7 or windows Server 2008 R2 if (os.wProductType == VER_NT_WORKSTATION)
{
versionName = Windows7;
}
else
{
versionName = WindowsServer2008R2;
}
}
else
if (minorVersion == )
{ // Vista or windows server 2008
if (os.wProductType == VER_NT_WORKSTATION)
{
versionName = WindowsVista;
}
else
{
versionName = WindowsServer2008;
}
} break;
// Handle Win2003, XP, Win2000
case :
switch (minorVersion)
{
case :
if ((os.wSuiteMask & VER_SUITE_STORAGE_SERVER) == VER_SUITE_STORAGE_SERVER)
{
versionName = WindowsServer2003Storage;
}
else if ((os.wSuiteMask & VER_SUITE_COMPUTE_SERVER) == VER_SUITE_COMPUTE_SERVER)
{
versionName = WindowsServer2003ComputeClusterEdition;
}
else if ((os.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
{
versionName = WindowsServer2003DatacenterEdition;
}
else if ((os.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
{
versionName = WindowsServer2003EnterpriseEdition;
}
else if ((os.wSuiteMask & VER_SUITE_BLADE) == VER_SUITE_BLADE)
{
versionName = WindowsServer2003WebEdition;
}
else
{
versionName = WindowsServer2003StandardEdition;
}
break;
case :
if ((os.wSuiteMask & VER_SUITE_PERSONAL) == VER_SUITE_PERSONAL)
{
versionName = WindowsXPHomeEdition;
}
else
{
versionName = WindowsXPProfessional;
}
break;
case :
if ((os.wProductType & VER_NT_WORKSTATION) == VER_NT_WORKSTATION)
{
versionName = Windows2000Professional;
}
else if ((os.wSuiteMask & VER_SUITE_DATACENTER) == VER_SUITE_DATACENTER)
{
versionName = Windows2000DatacenterServer;
}
else if ((os.wSuiteMask & VER_SUITE_ENTERPRISE) == VER_SUITE_ENTERPRISE)
{
versionName = Windows2000AdvancedServer;
}
else
{
versionName = Windows2000Server;
}
break;
}
break;
// Handle WinNT
case :
if (majorVersion == )
{
versionName = WindowsNT40;
}
break;
}
}
// Handle WinCE
else if (platform == PlatformID.WinCE)
{
versionName = WindowsCE;
} return versionName;
}
catch(PrerequisiteSoftwareValidationException)
{
throw;
}
catch(Exception ex)
{
// Wrap the exception into PrerequisiteSoftwareValidationException
throw new PrerequisiteSoftwareValidationException(
"An error occurred while getting the version.", ex);
}
} /// <summary>
/// <para>
/// Retrieves the Windows service pack.
/// </para>
/// </summary>
///
/// <returns>
/// The Windows service pack (can not be null, empty if no service packs are installed)
/// </returns>
///
/// <exception cref="PrerequisiteSoftwareValidationException">
/// If an error occurred while getting the version of the service pack.
/// </exception>
public static string GetServicePack()
{
try
{
return Environment.OSVersion.ServicePack;
}
catch(Exception ex)
{
// Wrap the exception into PrerequisiteSoftwareValidationException
throw new PrerequisiteSoftwareValidationException(
"An error occurred while getting the version of the service pack.", ex);
}
}
}
}

Manifest 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10
http://www.itdadao.com/article/227158/
https://msdn.microsoft.com/en-us/library/dn481241(v=vs.85).aspx
-->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!--This Id value indicates the application supports Windows 8.1 functionality-->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
<assemblyIdentity type="win32"
name="SampleCode.GetWindowsVersionInfo"
version="1.0.0.0"
processorArchitecture="x86"
publicKeyToken="0000000000000000"
/>
</assembly>

VS项目截取:

C# 获取并判断操作系统版本,解决Win10、 Windows Server 2012 R2 读取失败的方案的更多相关文章

  1. System x 服务器制作ServerGuide U盘安装Windows Server 2012 R2操作系统

    以下内容来源于:联想官方知识库  http://iknow.lenovo.com.cn/detail/dc_154773.html 本例介绍以U盘方式,通过ServerGuide引导在System x ...

  2. 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法

    现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...

  3. Windows server 2012 R2 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同

    Windows server 2012 R2 解决“无法完成域加入,原因是试图加入的域的SID与本计算机的SID相同.”使用克隆的系统时,加域是出现如下问题.“无法完成域加入,原因是试图加入的域的SI ...

  4. 微软操作系统 Windows Server 2012 R2 官方原版镜像

    微软操作系统 Windows Server 2012 R2 官方原版镜像 Windows Server 2012 R2 是由微软公司(Microsoft)设计开发的新一代的服务器专属操作系统,其核心版 ...

  5. 解决Windows Server 2012 R2 Datacenter云服务器无法运行opencv python程序的问题

    写了个基于opencv的python程序,pyinstaller 32位机打包后在win7/win10 32/64正常运行,在Windows Server 2012 R2 Datacenter云服务器 ...

  6. 分享Windows Server 2012 R2的获取正版密钥方法

    然后使用“我有ISIC卡”验证,目前可用号码:S420546009858. 分享Windows Server 2012 R2的获取正版密钥方法. 首先登陆dreamspark注册一个账号https:/ ...

  7. 解决 U盘安装Windows Server 2012 R2 报错 Windows 无法打开所需的文件 Sources\install.wim

    报错原因: 使用UltraISO等软件刻录镜像时默认使用FAT32文件系统,该系统不支持大于4G的文件, 而Server 2012 R2的安装文件install.wim为5.12G,固安装失败. 解决 ...

  8. 如何解决在Windows Server 2008 R2 上安装证书服务重启后出现 CertificationAuthority 91错误事件

    很久都没写什么博客了,前一段时间学习2008 R2时,在自己的电脑上同时安装AD 和证书 往往会出现一个CertificationAuthority错误,如下: 产生问题的主要原因是: 证书服务器与D ...

  9. 解决Windows server 2012 R2 系统使用IIS8浏览Asp程序出现"An error occurred on the server when processing the URL"错误

    进入IIS并将ASP里的“Send Error To Browser”设置为True后点击Appley保存即可 原因是IIS里的Asp设置禁用上当错误信息发送给浏览器,只要启用即可 如果没有Asp选项 ...

随机推荐

  1. AI相关 TensorFlow -卷积神经网络 踩坑日记之一

    上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...

  2. js中的数字格式变成货币类型的格式

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. 禁用自动映射的 Exchange 邮箱

    客户最近询问他们无法从用户的Outlook配置文件中删除邮箱.在这种情况下,它们是共享邮箱,并出现在Outlook的左窗格中.但原因和解决方法同样适用于用户邮箱.并且 无论用户邮箱在本地 Exchan ...

  4. vijos1760题解

    题目: 现在有n个人,题目给出了他们每个人所在市县的编号.他们站在一个从左向右的队伍中.小L不在队列中.他想找到一个长度不超过D的区域,使他能够找到最多的不同地方的朋友.要求输出能找到的朋友所在不同市 ...

  5. Chrome浏览器扩展开发系列之十三:消息传递Message

    由于content scripts运行在Web页面的上下文中,属于Web页面的组成部分,而不是Google Chrome扩展程序.但是content scripts又往往需要与Google Chrom ...

  6. Postgresql快速写入/读取大量数据(.net)

    环境及测试 使用.net驱动npgsql连接post数据库.配置:win10 x64, i5-4590, 16G DDR3, SSD 850EVO. postgresql 9.6.3,数据库与数据都安 ...

  7. 基于vue2.0的一个豆瓣电影App

    1.搭建项目框架 使用vue-cli 没安装的需要先安装 npm intall -g vue-cli 使用vue-cli生成项目框架 vue init webpack-simple vue-movie ...

  8. 0001.如何在Windows7(x64)上安装 Sharepoint2010 Fundation

    一.修改Config.xml文件 到目录:"C:\Program Files (x86)\MSECache\SharePoint2010\Files\Setup"下去修改confi ...

  9. ChartCtrl源码剖析之——CChartObject类

    首先,做一些简单的铺垫,目前针对ChartCtrl源码的剖析只针对V.15版本.名义上说是剖析,倒不如说是记录下自己针对该控件的理解,非常感谢Cedric Moonen大神,一切的功劳与掌声都该赠予给 ...

  10. 使用软件开发的部分思想,帮助HR处理Excel。

    前言 上周末,XX给我抱怨:因为计算绩效奖金,把2个人的工资发错了,还被扣了500元.问的缘由得知,她每个月要处理十来个excel表格,每次都要手动修改里面的值,如果修改了一处,其他地方也要修改,然后 ...