C# Windows アプリケーションで Windows 8 オペレーティング システムを検出し、いくつかの設定を行う必要があります。を使用して Windows 7 を検出できることはわかっていますが、Windows Environment.OSVersion
8 はどのように検出できますか?
前もって感謝します。
次の質問への回答を確認してください: 「わかりやすい」OS バージョン名を取得するにはどうすればよいですか?
引用された答え:
WMI を使用して製品名 (「Microsoft® Windows Server® 2008 Enterprise」) を取得できます。
using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First();
return name != null ? name.ToString() : "Unknown";
次のように構造体を宣言することから始めます。
[StructLayout(LayoutKind.Sequential)]
public struct OsVersionInfoEx
{
public int dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szCSDVersion;
public UInt16 wServicePackMajor;
public UInt16 wServicePackMinor;
public UInt16 wSuiteMask;
public byte wProductType;
public byte wReserved;
}
この using ステートメントが必要になります。
using System.Runtime.InteropServices;
関連するクラスの先頭で、次のように宣言します。
[DllImport("kernel32", EntryPoint = "GetVersionEx")]
static extern bool GetVersionEx(ref OsVersionInfoEx osVersionInfoEx);
次のようにコードを呼び出します。
const int VER_NT_WORKSTATION = 1;
var osInfoEx = new OsVersionInfoEx();
osInfoEx.dwOSVersionInfoSize = Marshal.SizeOf(osInfoEx);
try
{
if (!GetVersionEx(ref osInfoEx))
{
throw(new Exception("Could not determine OS Version"));
}
if (osInfoEx.dwMajorVersion == 6 && osInfoEx.dwMinorVersion == 2
&& osInfoEx.wProductType == VER_NT_WORKSTATION)
MessageBox.Show("You've Got windows 8");
}
catch (Exception)
{
throw;
}
私が持っているWindows 8のバージョンでしか確認できないため、これが正しいかどうかはわかりません。
int major = Environment.OSVersion.Version.Major;
int minor = Environment.OSVersion.Version.Minor;
if ((major >= 6) && (minor >= 2))
{
//do work here
}