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;
[Financialas (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;
[Financialas (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} operator 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 "windwoperating 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! ";
}
}
}
}