C # How to obtain the WINDOWS version

Source: Internet
Author: User

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! ";
}
}
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.