Как получить версию Windows, используя WinAPI?

code: #csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace ConsoleApplication1
{
    [StructLayout( LayoutKind.Sequential, CharSet = CharSet.Auto )]
    public class OSVersionInfo
    {
        public int dwOSVersionInfoSize;
        public uint dwMajorVersion;
        public uint dwMinorVersion;
        public uint dwBuildNumber;
        public uint dwPlatformId;
        [MarshalAs( UnmanagedType.ByValTStr, SizeConst = 128 )]
        public String szCSDVersion;
        public ushort  wServicePackMajor;
        public ushort  wServicePackMinor;
        public ushort  wSuiteMask;
        public byte  wProductType;
        public byte  wReserved;
    }
 
    class Kernel
    {
        [DllImport( "kernel32.dll", SetLastError = true, CharSet = CharSet.Auto )]
        public static extern bool GetVersionEx ( [In, Out] OSVersionInfo info );
    }
}
code: #csharp
using System;
using System.Runtime.InteropServices;
 
namespace ConsoleApplication1
{
 
    class Program
    {
        static void Main ( string[] args ) {
            var version = new OSVersionInfo();
            version.dwOSVersionInfoSize = Marshal.SizeOf( version );
            Kernel.GetVersionEx( version );
 
            if ( Marshal.GetLastWin32Error() != 0 )
                Console.WriteLine( Marshal.GetLastWin32Error() );
 
            Console.WriteLine( version.dwBuildNumber );
            Console.ReadKey();
        }
    }
}
Поделиться:

Похожие статьи: