【问题标题】:How to determine which version of Windows?如何确定是哪个版本的 Windows?
【发布时间】:2011-09-14 17:00:54
【问题描述】:
  1. 如何确定Windows的版本? WinXP、Vista 或 7 等。
  2. 32 位还是 64 位?

UPD:适用于 .Net 2.0 - 3.5

【问题讨论】:

  • 请注意检查 32 位或 64 位,因为您的进程可能与操作系统的位数不同

标签: c# .net-3.5 .net-2.0


【解决方案1】:

您正在寻找Environment.OSVersionEnvironment.Is64BitProcessEnvironment.Is64BitOperatingSystem 属性。

.Net 4.0之前,可以通过IntPtr.Size是否为8来判断进程是否为64位,通过@987654321可以判断操作系统是否为64位@:

public static bool Is64BitProcess
{
    get { return IntPtr.Size == 8; }
}

public static bool Is64BitOperatingSystem
{
    get
    {
        // Clearly if this is a 64-bit process we must be on a 64-bit OS.
        if (Is64BitProcess)
            return true;
        // Ok, so we are a 32-bit process, but is the OS 64-bit?
        // If we are running under Wow64 than the OS is 64-bit.
        bool isWow64;
        return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
    }
}

static bool ModuleContainsFunction(string moduleName, string methodName)
{
    IntPtr hModule = GetModuleHandle(moduleName);
    if (hModule != IntPtr.Zero)
        return GetProcAddress(hModule, methodName) != IntPtr.Zero;
    return false;
}

[DllImport("kernel32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);

【讨论】:

  • AFAIK,OperatingSystem 类型直接不公开字段来确定位数。我认为,需要对已知术语进行一些验证。
  • @MrD: Is64BitOperatingSystem
  • @Slaks:是的,刚刚注意到更新 - 不知道Environment 拥有这样的房产。好东西。这一定是 .NET4 的新内容?
  • 我的 Windows 版本是“Microsoft Windows NT 6.1.7600.0”。如何确定是 XP、Vista 还是 7?
  • XP 是 5.1,Vista 是 6.0,7 是 6.1
【解决方案2】:

你可以的

System.OperatingSystem osInfo = System.Environment.OSVersion;

看看this

【讨论】:

    【解决方案3】:

    看看Environment.OSVersionEnvironment.Is64BitOperatingSystem

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 2016-12-16
      • 2019-09-04
      • 2010-11-17
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多