Mega Code Archive

 
Categories / C# / Windows
 

Get OS Version from kernel32 dll

using System; using System.Runtime.InteropServices; public class Starter {     public static void Main() {         API.OSVERSIONINFO info = new API.OSVERSIONINFO();         info.dwOSVersionInfoSize = Marshal.SizeOf(info);         bool resp = API.GetVersionEx(ref info);         if (resp == false) {             Console.WriteLine("GetVersion failed");         }         Console.WriteLine("{0}.{1}.{2}",             info.dwMajorVersion,             info.dwMinorVersion,             info.dwBuildNumber);     } } public class API {     [DllImport("kernel32.dll")]     public static extern         bool GetVersionEx(ref OSVERSIONINFO lpVersionInfo);     [StructLayout(LayoutKind.Sequential)]     public struct OSVERSIONINFO {         public System.Int32 dwOSVersionInfoSize;         public System.Int32 dwMajorVersion;         public System.Int32 dwMinorVersion;         public System.Int32 dwBuildNumber;         public System.Int32 dwPlatformId;         [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]         public String szCSDVersion;     } }