[英]How to know if my current operating system is Windows Vista in C#?
[英]How to detect Windows 8 Operating system using C# 4.0?
我必須在C#Windows應用程序中檢測Windows 8操作系統並進行一些設置。 我知道我們可以使用Environment.OSVersion
檢測Windows 7,但是如何檢測Windows 8?
提前致謝。
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8version)
{
// its win8 or higher.
}
好吧,伙計們,在我看來,這段代碼已被微軟本身標記為棄用。 我在此處留下鏈接,以便您可以閱讀更多相關信息。
簡而言之,它說:
對於Windows 8及更高版本,總是會有相同的版本號(6,2,9200,0) 。 而不是尋找Windows版本,去尋找你試圖解決的實際功能。
Windows 8或更新版本:
bool IsWindows8OrNewer()
{
var os = Environment.OSVersion;
return os.Platform == PlatformID.Win32NT &&
(os.Version.Major > 6 || (os.Version.Major == 6 && os.Version.Minor >= 2));
}
檢查以下問題的答案: 如何獲得“友好”操作系統版本名稱?
引用答案:
您可以使用WMI獲取產品名稱(“Microsoft®WindowsServer®2008Enterprise”):
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 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.