C#如何获得 WINDOWS 版本号
using System;
using System.Runtime.InteropServices;
namespace GetWindowsVersion
{
[ StructLayout( LayoutKind.Sequential )]
public class OSVersionInfo
{
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String versionString;
}
[ StructLayout( LayoutKind.Sequential )]
public struct OSVersionInfo2
{
public int OSVersionInfoSize;
public int MajorVersion;
public int MinorVersion;
public int BuildNumber;
public int PlatformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String versionString;
}
public class LibWrap
{
[ DllImport( "kernel32" )]
public static extern bool GetVersionEx( [In, Out] OSVersionInfo osvi );
[ DllImport( "kernel32", EntryPoint="GetVersionEx" )]
public static extern bool GetVersionEx2( ref OSVersionInfo2 osvi );
}
public class App
{
public static void Main()
{
Console.WriteLine( " Passing OSVersionInfo as class" );
OSVersionInfo osvi = new OSVersionInfo();
osvi.OSVersionInfoSize = Marshal.SizeOf( osvi );
LibWrap.GetVersionEx( osvi );
Console.WriteLine( "Class size: {0} Operation System : {1} Pack: {2}", osvi.OSVersionInfoSize, OpSysName(osvi.MajorVersion, osvi.MinorVersion,osvi.PlatformId),osvi.versionString);
Console.WriteLine("{0}", osvi.PlatformId);
Console.WriteLine( " Passing OSVersionInfo as struct" );
OSVersionInfo2 osvi2 = new OSVersionInfo2();
osvi2.OSVersionInfoSize = Marshal.SizeOf( osvi2 );
LibWrap.GetVersionEx2( ref osvi2 );
Console.WriteLine( "Static size: {0} Operation System : {1} Pack: {2}", osvi2.OSVersionInfoSize, OpSysName(osvi2.MajorVersion, osvi2.MinorVersion,osvi2.PlatformId),osvi2.versionString);
}
public static String OpSysName(int MajorVersion,int MinorVersion ,int PlatformId)
{
String str_opn =String.Format("{0}.{1}",MajorVersion,MinorVersion);
switch(str_opn)
{
case "4.0":
return win95_nt40(PlatformId);
case "4.10":
return "Windows 98";
case "4.90":
return "Windows Me";
case "3.51":
return "Windows NT 3.51";
case "5.0":
return "Windwos 2000";
case "5.1":
return "Windwos XP";
case "5.2":
return "Windows Server 2003 family";
default:
return "This windows version is not distinguish!";
}
}
public static String win95_nt40(int PlatformId)
{
switch(PlatformId)
{
case 1:
return "Windows 95";
case 2:
return "Windows NT 4.0";
default:
return "This windows version is not distinguish!";
}
}
}
}
转载:http://www.2cto.com/kf/200906/39061.html
使用GetVersionEx传递例如以下结构来获得此信息
typedef struct _OSVERSIONINFOEX {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[128];
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask; //此成员能够用来作为推断home 还是professional的根据
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;
关于操作系统的其它的信息你都能够在此结构中找到,详细使用參见MSDN的帮助
不怎么用C#,现学现卖给你写了一段代码,(代码中有不合理的地方,自己參照改动,实在没用过C#)例如以下:
主要代码例如以下:
using System.Runtime.InteropServices;
上述结构在C#中定义例如以下:
[ StructLayout( LayoutKind.Sequential )]
public class OSVersionInfo
{
public int OSVersionInfoSize;
public int majorVersion;
public int minorVersion;
public int buildNumber;
public int platformId;
[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=128 )]
public String versionString;
public Int16 wServicePackMajor;
public Int16 wServicePackMinor;
public Int16 wSuiteMask;
public Byte wProductType;
public Byte wReserved;
}
public class GetSysApi
{
[ DllImport( "Kernel32.dll" )]
public static extern bool GetVersionEx( [In, Out] OSVersionInfo osvi );
}
private void button2_Click(object sender, System.EventArgs e)
{
OSVersionInfo osvi = new OSVersionInfo();
osvi.OSVersionInfoSize = Marshal.SizeOf( osvi );
GetSysApi.GetVersionEx(osvi);
string tm = osvi.buildNumber+osvi.majorVersion+osvi.minorVersion+
osvi.platformId +osvi.versionString+osvi.wProductType+
osvi.wReserved+osvi.wSuiteMask;
MessageBox.Show(tm);//依据那些成员变量,有足够的信息够你对操作系统进行推断
//我这里仅仅写了你需求的关于windows xp professional或者windows xp home的推断
if ( osvi.wProductType == 1)
{
if( osvi.majorVersion == 4 )
MessageBox.Show( "Workstation 4.0 ");
else if(( osvi.wSuiteMask & 512 )!=0)
MessageBox.Show("Win Xp Home Edition " );
else MessageBox.Show("Win Xp Professional " );
}
}
来自:http://bbs.csdn.net/topics/90504765
C#如何获得 WINDOWS 版本号的更多相关文章
- 超具体Windows版本号编译执行React Native官方实例UIExplorer项目(多图慎入)
),React Native技术交流4群(458982758).请不要反复加群! 欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客右側欢迎微信扫描关注订阅号,移动技术干货,精彩文 ...
- 开源企业IM-免费企业即时通讯-ENTBOOST V2014.183 Windows版本号正式宣布
ENTBOOST,VERSION 2014.183 Windows(点击下载)版本号公布.主要添加PC端P2P(点对点)文件传输功能:公布安卓Android手机clientAPP 1.0版本号.公布苹 ...
- 推断Windows版本号新方法
曾经在代码中推断用户的Windows操作系统版本号都是通过GetVersion或GetVersionEx获取版本号号,然后比較. 今天偶然发如今新的Visual Studio提供了新的函数来推断系统版 ...
- 获取windows版本号
原文:https://blog.csdn.net/justFWD/article/details/44856277 内容整理如下,点击跳至指定内容: manifest文件加上compatibility ...
- 查看Windows版本号
1.Win+R,在运行中输入:msinfo32 2.在cmd中输入:ver 3.注册表(regedit)中:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows ...
- 开源企业IM-免费企业即时通讯-ENTBOOST V2014.180 Windows版本号正式公布
ENTBOOST,VERSION 2014.180 Linux版本号公布,主要添加企业IM应用集成功能,完好安卓SDK功能及部分BUG修正. 下一版本号公布时间.7月15日.敬请关注. ENTBOOS ...
- 开源企业IM-免费企业即时通讯-ENTBOOST V2014.177 Windows版本号正式公布
ENTBOOST,VERSION 2014.177 LINUX 版本号公布.主要添加Android安卓手机开发接口.企业IM接口,JQUERY开发接口,PCclient部分BUG修正: 下版本号更新时 ...
- 显示Windows版本号
实现效果: 知识运用: PaintDesktopVersion键 实现代码: private void button1_Click(object sender, EventArgs e) { Regi ...
- 开源企业IM,免费企业即时通讯软件-ENTBOOST云通讯平台Windows(r174)版本号公布
经过恩布团队全体成员的不懈努力,依照原定计划,最终在今天(5月14日)公布第二个开源版本号,恩布企业互联IM,ENTBOOST 0.2.0(r174beta)Windows版本号:主要功能支持文本.表 ...
随机推荐
- Powershell Mail module, 发送outbox 里的全部邮件(一个.csv文件代表一封邮件)
把creating mail代码写到调用处,往outbox写入 mailxxx.csv文件,入面记录了邮件的主要内容 写入 #template $TMP = IMPORT-CSV "$($d ...
- UVa-Palindromes
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- hdu2444(判二分图+最大匹配)
传送门:The Accomodation of Students 题意:有n个学生,m对相互认识的,问能否分成两队,使得每对中没有相互认识的,如果可以求最大匹配,否则输出No. 分析:判断二分图用染色 ...
- jQuery 自学笔记—5 遍历
什么是遍历? jQuery 遍历,意为“移动”,用于根据其相对于其他元素的关系来“查找”(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您期望的元素为止. 下图展示了一个家族树. ...
- xcode 改动整个项目名
目标为:将项目名XCD4改成xcd5. 先上结果图:==> 1.在左側的导航区域点击两次项目名,两次间隔时间略微长些,项目名会变成可编辑状态. 将名称改动为xcd5后按enter键弹出一个对话框 ...
- 深入理解Tomcat系列之一:系统架构(转)
前言 Tomcat是Apache基金组织下的开源项目,性质是一个Web服务器.下面这种情况很普遍:在eclipse床架一个web项目并部署到Tomcat中,启动tomcat,在浏览器中输入一个类似ht ...
- 23、Cocos2dx 3.0游戏开发找小三之粒子系统:你那里下雪了吗?
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30485919 春雨惊春清谷天,夏满芒夏暑相连, 秋处 ...
- hdu 4529 Double Dealing (置换群)
# include <stdio.h> # include <algorithm> # include <string.h> using namespace std ...
- 利用try-catch判断变量是已声明未声明还是未赋值
原文 利用try-catch判断变量是已声明未声明还是未赋值 这篇文章主要介绍了利用try-catch判断变量是已声明未赋值还是未声明,需要的朋友可以参考下 目的是如果一个变量是已声明未赋值,就可以直 ...
- 逆向 Framework.jar
Ref:http://www.addictivetips.com/mobile/what-is-odex-and-deodex-in-android-complete-guide/ Ref:http: ...