Get OS version information - CSharp Operating System

CSharp examples for Operating System:Windows

Description

Get OS version information

Demo Code


using System;// w w  w.j  av  a 2s  . c om
using System.Runtime.InteropServices;

class MainClass
{
    // Declare the external function.
    [DllImport("kernel32.dll")]
    public static extern bool GetVersionEx([In, Out] OSVersionInfo osvi);

    static void Main(string[] args)
    {
        OSVersionInfo osvi = new OSVersionInfo();
        osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi);

        GetVersionEx(osvi);

        Console.WriteLine("Class size: " + osvi.dwOSVersionInfoSize);
        Console.WriteLine("Major Version: " + osvi.dwMajorVersion);
        Console.WriteLine("Minor Version: " + osvi.dwMinorVersion);
        Console.WriteLine("Build Number: " + osvi.dwBuildNumber);
        Console.WriteLine("Platform Id: " + osvi.dwPlatformId);
        Console.WriteLine("CSD Version: " + osvi.szCSDVersion);
        Console.WriteLine("Platform: " + Environment.OSVersion.Platform);
        Console.WriteLine("Version: " + Environment.OSVersion.Version);

    }
}

// Define the structure and specify the layout type as sequential.
[StructLayout(LayoutKind.Sequential)]
public class OSVersionInfo
{
    public int dwOSVersionInfoSize;
    public int dwMajorVersion;
    public int dwMinorVersion;
    public int dwBuildNumber;
    public int dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public String szCSDVersion;
}

Result


Related Tutorials