Windowsバージョンの取得方法
アプリケーションが動作しているWindows環境について、Windowsの種類(Windows XP, Vista, 7, Server 2003, Server 2008など)とService Packを取得する方法を調べたのでメモしておきます。
まず、バージョン判定の考え方ですが、これはMicrosoftのサイトに載っていました。
- OSVERSIONINFOEX Structure (Windows)
Operating system | Version number | dwMajorVersion | dwMinorVersion | Other |
---|---|---|---|---|
Windows 7 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2008 R2 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Server 2008 | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Vista | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2003 R2 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) != 0 |
Windows Home Server | 5.2 | 5 | 2 | OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVER |
Windows Server 2003 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) == 0 |
Windows XP Professional x64 Edition | 5.2 | 5 | 2 | (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) |
Windows XP | 5.1 | 5 | 1 | Not applicable |
Windows 2000 | 5.0 | 5 | 0 | Not applicable |
さらに、上記表をC++言語で実装したサンプルも記述されていました。
- Getting the System Version (Windows)
あとは、C#に置き換えて終わりです。次のようになりました。ただし、エディションの取得は割愛しています。
動作確認した環境は、Windows 7のみです。他の環境では動作させていないので、バグなどにより動作しない場合があるかも知れません。もし、動作確認した方がいましたら、コメントに記述して頂けると助かります。
using System; using System.Runtime.InteropServices; #region 構造体、定数、DLL定義 [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; } // GetVersionEx private const Int32 VER_NT_WORKSTATION = 1; private const Int32 VER_NT_DOMAIN_CONTROLLER = 2; private const Int32 VER_NT_SERVER = 3; private const Int32 VER_SUITE_SMALLBUSINESS = 1; private const Int32 VER_SUITE_ENTERPRISE = 2; private const Int32 VER_SUITE_TERMINAL = 16; private const Int32 VER_SUITE_DATACENTER = 128; private const Int32 VER_SUITE_SINGLEUSERTS = 256; private const Int32 VER_SUITE_PERSONAL = 512; private const Int32 VER_SUITE_BLADE = 1024; private const Int32 VER_SUITE_STORAGE_SERVER = 8192; //0x00002000; private const Int32 VER_SUITE_WH_SERVER = 32768; //0x00008000; // GetSystemMetrics private const Int32 SM_SERVERR2 = 89; // エラー private const string ERR = "取得失敗"; [DllImport("kernel32.dll")] private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo); [DllImport("user32.dll")] private static extern int GetSystemMetrics(Int32 nIndex); #endregion public string GetOSDisplayString() { string result = ERR; OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX(); OperatingSystem osInfo = Environment.OSVersion; osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX)); if (!GetVersionEx(ref osVersionInfo)) { return ERR; } if (osInfo.Platform == PlatformID.Win32NT) { if (osInfo.Version.Major > 4) { result = "Microsoft "; } if (osInfo.Version.Major == 6) { if (osInfo.Version.Minor == 0) { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { result += "Windows Vista"; } else { result += "Windows Server 2008"; } } else if (osInfo.Version.Minor == 1) { if (osVersionInfo.wProductType == VER_NT_WORKSTATION) { result += "Windows 7"; } else { result += "Windows Server 2008 R2"; } } } else if (osInfo.Version.Major == 5) { if (osInfo.Version.Minor == 2) { if (GetSystemMetrics(SM_SERVERR2) != 0) { result += "Windows Server 2003 R2"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_STORAGE_SERVER) > 0) { result += "Windows Storage Server 2003"; } else if ((osVersionInfo.wSuiteMask & VER_SUITE_WH_SERVER) > 0) { result += "Windows Home Server"; } else if (osVersionInfo.wProductType == VER_NT_WORKSTATION && IntPtr.Size == 8) { result += "Windows XP Professional x64 Edition"; } else { result += "Windows Server 2003"; } // エディション取得処理は省略 } else if (osInfo.Version.Minor == 1) { result += "Windows XP"; } else if (osInfo.Version.Minor == 0) { result += "Windows 2000"; } } // Include service pack (if any) if (osVersionInfo.szCSDVersion.Length > 0) { result += " " + osVersionInfo.szCSDVersion; } // Architecture if (IntPtr.Size == 4) { result += ", 32-bit"; } else { result += ", 64-bit"; } } else if (osInfo.Platform == PlatformID.Win32Windows) { result = "This sample does not support this version of Windows."; } return result; }
C++言語をC#言語に置き換えする際に、参考にしたページは次になります。
- OS Name, Version & Product Type - CodeProject
- アプリケーションが64ビットで動いているか調べる、OSが64ビットか調べる: .NET Tips: C#, VB.NET, Visual Studio