C# 这是确定操作系统架构的好方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is this a good way to determine OS Architecture?
提问by Jeremy Privett
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote up a method using the registry to try and determine whether or not the current system is a 32 or 64bit system.
由于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;
}
}
It's worked out pretty well for us so far, but I'm not sure how much I like looking through the registry. Is this a pretty standard practice or is there a better method?
到目前为止,它对我们来说效果很好,但我不确定我有多喜欢查看注册表。这是一个非常标准的做法还是有更好的方法?
Edit: Wow, that code looks a lot prettier in the preview. I'll consider linking to a pastebin or something, next time.
编辑:哇,该代码在预览中看起来更漂亮。下次我会考虑链接到 pastebin 或其他东西。
采纳答案by Kev
Take a look at Raymond Chens solution:
看看Raymond Chen的解决方案:
How to detect programmatically whether you are running on 64-bit Windows
如何以编程方式检测您是否在 64 位 Windows 上运行
and here's the PINVOKE for .NET:
这是 .NET 的 PINVOKE:
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.
更新:我对检查“x86”有疑问。谁能说英特尔或 AMD 的下一个 32 位处理器可能被指定为什么。概率很低,但有风险。您应该要求操作系统通过正确的 API 来确定这一点,而不是通过查询可能被认为对外界不透明的操作系统版本/平台特定值。问自己这些问题,1 - MS 是否正确记录了相关的注册表项,2 - 如果是,他们会提供可能值的明确列表,保证允许您作为开发人员在是否运行之间做出明智的决定32 位或 64 位。如果答案是否定的,那么调用 API,是的,它是一个但更冗长的但它是有记录的和确定的。
回答by Greg Hurlman
Looking into the registry is perfectly valid, so long as you can be sure that the user of the application will always have access to what you need.
查看注册表是完全有效的,只要您可以确保应用程序的用户始终可以访问您需要的内容。
回答by Curt Hagenlocher
The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size.
在 .NET 下测试 64 位的最简单方法是检查 IntPtr.Size 的值。
EDIT: Doh! This will tell you whether or not the current process is 64-bit, not the OS as a whole. Sorry!
编辑:哦!这将告诉您当前进程是否为 64 位,而不是整个操作系统。对不起!
回答by Jeremy Privett
The easiest way to test for 64-bit under .NET is to check the value of IntPtr.Size.
在 .NET 下测试 64 位的最简单方法是检查 IntPtr.Size 的值。
I believe the value of IntPtr.Size is 4 for a 32bit app that's running under WOW, isn't it?
对于在 WOW 下运行的 32 位应用程序,我相信 IntPtr.Size 的值是 4,不是吗?
Edit: @Edit: Yeah. :)
编辑:@Edit:是的。:)