这是确定操作系统架构的好方法吗?

由于WMI类Win32_OperatingSystem仅包含Windows Vista中的OSArchitecture,因此我很快写了一种使用注册表的方法来尝试确定当前系统是32位还是64位系统。

private Boolean is64BitOperatingSystem()
{
    RegistryKey localEnvironment = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment");
    String processorArchitecture = (String) localEnvironment.GetValue("PROCESSOR_ARCHITECTURE");

    if (processorArchitecture.Equals("x86")) {
        return false;
    }
    else {
        return true;
    }
}

到目前为止,它对我们来说算得非常好,但我不确定我喜欢通过注册表浏览多少。这是一个非常标准的做法还是有更好的方法?

Edit: Wow, that code looks a lot prettier in the preview. I'll consider linking to a pastebin or something, next time.

0

4 答案

查看注册表是完全有效的,只要您可以确保应用程序的用户始终可以访问您所需的内容即可。

0
额外
在.NET下测试64位的最简单方法是检查IntPtr.Size的值。

我相信在WOW下运行的32位应用程序的IntPtr.Size的值是4,不是吗?

Edit: @Edit: Yeah. :)

0
额外

看看Raymond Chens解决方案:

如何以编程方式检测您是否在64位Windows上运行

这里是.NET的PINVOKE:

IsWow64Process(kernel32)

Update: I'd take issue with checking for 'x86'. Who's to say what intel's or AMD's next 32 bit processor may be designated as. The probability is low but it is a risk. You should ask the OS to determine this via the correct API's, not by querying what could be a OS version/platform specific value that may be considered opaque to the outside world. Ask yourself the questions, 1 - is the registry entry concerned properly documented by MS, 2 - If it is do they provide a definitive list of possible values that is guaranteed to permit you as a developer to make the informed decision between whether you are running 32 bit or 64 bit. If the answer is no, then call the API's, yeah it's a but more long winded but it is documented and definitive.

0
额外

在.NET下测试64位的最简单方法是检查IntPtr.Size的值。

编辑:哦!这会告诉你当前进程是否是64位的,而不是整个操作系统。抱歉!

0
额外