Get Windows version, Architecture and Service pack

Let’s see how can we get Windows Version, CPU architecture and Windows Service Pack in Windows using C#.

We are going to use GetVersionEx and OSVERSIONINFOEX structure from Windows API.

Windows API:

[StructLayout(LayoutKind.Sequential)]
private struct OSVERSIONINFOEX
{
	public int dwOSVersionInfoSize;
	public int dwMajorVersion;
	public int dwMinorVersion;
	public int dwBuildNumber;
	public int dwPlatformId;
	[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
	public string szCSDVersion;
	public short wServicePackMajor;
	public short wServicePackMinor;
	public short wSuiteMask;
	public byte wProductType;
	public byte wReserved;
}
 
[DllImport("kernel32.dll")]
private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);

Constants:

private const int VER_NT_WORKSTATION = 0x0000001;
private const int VER_NT_DOMAIN_CONTROLLER = 0x0000002;
private const int VER_NT_SERVER = 0x0000003;
private const int VER_PLATFORM_WIN32_NT = 2;

Method to print information:

void GetWinVer()
{
    try
    {
   	String osname = String.Empty;
	OSVERSIONINFOEX os = new OSVERSIONINFOEX();
	os.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
	if (!GetVersionEx(ref os)) return;
 
	if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
	{
		if (os.wProductType == VER_NT_WORKSTATION)
		{
			if (os.dwMajorVersion == 5 && os.dwMinorVersion == 0)
				osname = "Windows 2000";
			else if (os.dwMajorVersion == 5 && os.dwMinorVersion == 1)
				osname = "Windows XP";
			else if (os.dwMajorVersion == 5 && os.dwMinorVersion == 2)
				osname = "Windows XP x64 PRO";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 0)
				osname = "Windows VISTA";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 1)
				osname = "Windows 7";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 2)
				osname = "Windows 8";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 3)
				osname = "Windows 8.1";
		        else if (os.dwMajorVersion == 10 && os.dwMinorVersion == 0)
				osname = "Windows 10";
		}
		else if (os.wProductType == VER_NT_DOMAIN_CONTROLLER ||
			os.wProductType == VER_NT_SERVER)
		{
			if (os.dwMajorVersion == 5 && os.dwMinorVersion == 2)
				osname = "Windows Server 2003";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 0)
				osname = "Windows Server 2008";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 1)
				osname = "Windows Server 2008 R2";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 2)
				osname = "Windows Server 2012";
			else if (os.dwMajorVersion == 6 && os.dwMinorVersion == 3)
				osname = "Windows Server 2012 R2";
			else if (os.dwMajorVersion == 10 && os.dwMinorVersion == 0)
				osname = "Windows Server 2010";
		}
	}
	osname += " ";
	ManagementObjectCollection.ManagementObjectEnumerator moe =
		new ManagementObjectSearcher("SELECT Architecture FROM Win32_Processor").Get().GetEnumerator();
	moe.MoveNext();
	ushort arch = (ushort)moe.Current["Architecture"];
 
	switch (arch)
	{
		case 0:
			osname += "x86"; break;
		case 6:
			osname += "Itanium"; break;
		case 9:
			osname += "x64"; break;
	}
	osname += " ";
	osname += Environment.OSVersion.ServicePack;
	Console.WriteLine(osname);
    }
    catch (Exception ex)
    {
	Console.WriteLine(ex.Message);
    }
}

Usage:

GetWinVer();

Prints:
for ex. Windows 7 x64 Service Pack 1

Categories: C# Tags: , , ,
%d bloggers like this: