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
版本号