如何使用.NET检测Windows 64位平台?
我使用以下代码来检测操作系统平台:
string os_platform = System.Environment.OSVersion.Platform.ToString();
在 .NET 2.0 C# 应用程序中, 返回“Win32NT”。 问题在于,即使在 Windows Vista 64 位上运行,它也会返回“Win32NT”。
有没有其他方法可以知道正确的平台(32 位或 64 位)?
请注意,当在 Windows 64 位上作为 32 位应用程序运行时,它还应该检测 64 位。
In a .NET 2.0 C# application I use the following code to detect the operating system platform:
string os_platform = System.Environment.OSVersion.Platform.ToString();
This returns "Win32NT". The problem is that it returns "Win32NT" even when running on Windows Vista 64-bit.
Is there any other method to know the correct platform (32 or 64 bit)?
Note that it should also detect 64 bit when run as a 32 bit application on Windows 64 bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
将以下代码包含到项目中的类中:
像这样使用它:
Include the following code into a class in your project:
Use it like so:
使用它来获取已安装的 Windows 体系结构:
Use this to get the installed Windows architecture:
鉴于所接受的答案非常复杂。 还有更简单的方法。 我的答案是 alexandrudicu 的答案的变体。
鉴于 64 位 Windows 在 Program Files (x86) 中安装 32 位应用程序,您可以使用环境变量(以弥补不同的本地化)检查该文件夹是否存在,
例如,
这对我来说更快、更简单。 鉴于我还希望根据操作系统版本访问该文件夹下的特定路径。
Given that the accepted answer is very complex. There are simpler ways. Mine is a variation of alexandrudicu's anaswer.
Given that 64-bit windows install 32-bit applications in Program Files (x86) you can check if that folder exists, using environment variables (to make up for different localizations)
e.g.
This for me is faster and simpler. Given that I also wish to access a specific path under that folder based on OS version.
这个问题是针对 .NET 2.0 的,但仍然出现在谷歌搜索中,这里没有人提到自从 .NET 标准 1.1 / .NET core 1.0 以来,现在有一种更好的方法来了解 CPU 架构:
理论上这应该能够x64 和 Arm64 之间的区别,尽管我自己没有测试过。
请参阅文档。
This question is for .NET 2.0 but still comes up in a google search, and nobody here mentionned that since .NET standard 1.1 / .NET core 1.0, there is now a better way to know the CPU architecture:
This should theoretically be able to differenciate between x64 and Arm64, though I didn't test it myself.
See the documentation.
抱歉,VB.NET 的代码很容易移植到 C#。
它在 Win7、x64 和 .Net Framework 4.8 上运行正常:
Sorry for code of VB.NET, but it easy to port to C#.
It works ok on Win7, x64 with .Net Framework 4.8:
享受 ;-)
Enjoy ;-)
只需查看“C:\Program Files (x86)”是否存在。 如果没有,那么您使用的是 32 位操作系统。 如果是,则操作系统是 64 位(Windows Vista 或 Windows 7)。 看起来很简单...
Just see if the "C:\Program Files (x86)" exists. If not, then you are on a 32 bit OS. If it does, then the OS is 64 bit (Windows Vista or Windows 7). It seems simple enough...
我使用:
这会获取应用程序启动的路径,以防您将其安装在计算机上的各个位置。 另外,您可以只使用一般的
C:\
路径,因为 99.9% 的计算机都将 Windows 安装在C:\
中。I use:
This gets the path where your application is launched in case you have it installed in various places on the computer. Also, you could just do the general
C:\
path since 99.9% of computers out there have Windows installed inC:\
..NET 4 在Environment 类中有两个新属性,Is64BitProcess 和 Is64BitOperatingSystem 。 有趣的是,如果您使用 Reflector,您会发现它们在 32 位和 32 位操作系统中的实现方式有所不同。 mscorlib 的 64 位版本。 32 位版本对 Is64BitProcess 返回 false,并通过 P/Invoke 对 Is64BitOperatingSystem 调用 IsWow64Process。 64 位版本仅对两者返回 true。
.NET 4 has two new properties in the Environment class, Is64BitProcess and Is64BitOperatingSystem. Interestingly, if you use Reflector you can see they are implemented differently in the 32-bit & 64-bit versions of mscorlib. The 32-bit version returns false for Is64BitProcess and calls IsWow64Process via P/Invoke for Is64BitOperatingSystem. The 64-bit version just returns true for both.
更新: 正如 Joel Coehoorn 和其他人建议的那样,从 .NET Framework 4.0 开始,您只需检查
Environment.Is64BitOperatingSystem
。如果在 64 位 Windows 上的 32 位 .NET Framework 2.0 中运行,IntPtr.Size 将不会返回正确的值(它将返回 32 位)。
正如微软的Raymond Chen所描述的,你必须首先检查是否在64位进程中运行(我认为在.NET中你可以通过检查IntPtr.Size来做到这一点),如果你在32位进程中运行,你仍然必须调用Win API函数IsWow64Process。 如果返回 true,则您正在 64 位 Windows 上的 32 位进程中运行。
微软的雷蒙德·陈:
如何以编程方式检测您是否在 64 位 Windows 上运行
我的解决方案:
UPDATE: As Joel Coehoorn and others suggest, starting at .NET Framework 4.0, you can just check
Environment.Is64BitOperatingSystem
.IntPtr.Size won't return the correct value if running in 32-bit .NET Framework 2.0 on 64-bit Windows (it would return 32-bit).
As Microsoft's Raymond Chen describes, you have to first check if running in a 64-bit process (I think in .NET you can do so by checking IntPtr.Size), and if you are running in a 32-bit process, you still have to call the Win API function IsWow64Process. If this returns true, you are running in a 32-bit process on 64-bit Windows.
Microsoft's Raymond Chen:
How to detect programmatically whether you are running on 64-bit Windows
My solution:
如果您使用 .NET Framework 4.0,这很简单:
请参阅Environment.Is64BitOperatingSystem 属性 (MSDN)。
If you're using .NET Framework 4.0, it's easy:
See Environment.Is64BitOperatingSystem Property (MSDN).
这只是 Bruno Lopez 上面建议的实现,但适用于 Win2k + 所有 WinXP 服务包。 只是想我会把它发布出来,这样其他人就不用用手滚动它了。 (本可以作为评论发布,但我是新用户!)
This is just an implementation of what's suggested above by Bruno Lopez, but works on Win2k + all WinXP service packs. Just figured I'd post it so other people didn't have roll it by hand. (would have posted as a comment, but I'm a new user!)
完整的答案是这样的(取自 stefan-mg、ripper234 和 BobbyShaftoe 的答案):
首先检查您是否处于 64 位进程中。 如果不是,请检查 32 位进程是否是 Wow64Process。
The full answer is this (taken from both stefan-mg, ripper234 and BobbyShaftoe's answer):
First check if you're in a 64 bit process. If you're not, check if the 32 bit process is a Wow64Process.
微软为此提供了一个代码示例:
http://1code.codeplex.com/ SourceControl/changeset/view/39074#842775
看起来像这样:
还有一个可用的 WMI 版本(用于测试远程计算机)。
Microsoft has put a code sample for this:
http://1code.codeplex.com/SourceControl/changeset/view/39074#842775
It looks like this:
There is a WMI version available as well (for testing remote machines).
来自 Chriz Yuen 博客
C# .Net 4.0 引入了两个新的环境属性
环境.Is64BitOperatingSystem;
环境.Is64BitProcess;
使用这两个属性时请小心。
在Windows 7 64位机器上测试
From Chriz Yuen blog
C# .Net 4.0 Introduced two new environment property
Environment.Is64BitOperatingSystem;
Environment.Is64BitProcess;
Please be careful when you use these both property.
Test on Windows 7 64bits Machine
您还可以检查
PROCESSOR_ARCHITECTURE
环境变量。它要么不存在,要么在 32 位 Windows 上设置为“x86”。
You can also check for the
PROCESSOR_ARCHITECTURE
environment variable.It either doesn't exist or is set to "x86" on 32-bit Windows.
最快的方法:
注意:这是非常直接的,只有当程序不强制执行为 32 位进程时(例如通过
项目设置中的true
)。Quickest way:
Note: this is very direct and works correctly on 64-bit only if the program does not force execution as a 32-bit process (e.g. through
<Prefer32Bit>true</Prefer32Bit>
in the project settings).尝试这个:
Try this:
@foobar:你是对的,这太容易了;)
在 99% 的情况下,系统管理员背景薄弱的开发人员最终无法意识到 Microsoft 一直为任何人提供枚举 Windows 的能力。
当涉及到这一点时,系统管理员总是会编写更好、更简单的代码。
不过,需要注意的一件事是,构建配置必须是 AnyCPU,此环境变量才能在正确的系统上返回正确的值:
这将在 32 位 Windows 上返回“X86”,在 32 位 Windows 上返回“AMD64” 64 位 Windows。
@foobar: You are right, it is too easy ;)
In 99% of the cases, developers with weak system administrator backgrounds ultimately fail to realize the power Microsoft has always provided for anyone to enumerate Windows.
System administrators will always write better and simpler code when it comes to such a point.
Nevertheless, one thing to note, build configuration must be AnyCPU for this environment variable to return the correct values on the correct systems:
This will return "X86" on 32-bit Windows, and "AMD64" on 64-bit Windows.
使用 dotPeek 有助于了解框架实际上是如何实现的。 考虑到这一点,这就是我的想法:
用法示例:
Using dotPeek helps to see how the framework actually does it. With that in mind, here's what I've come up with:
Example usage:
使用这两个环境变量(伪代码):
参考博文如何:检测进程位数。
Use these two environment variables (pseudo code):
Refer to the blog post HOWTO: Detect Process Bitness.
我在许多操作系统上成功使用了此检查:
无论操作系统的语言如何,此文件夹始终命名为“SysWOW64”。 这适用于 .NET Framework 1.1 或更高版本。
I used this check with success on many operating systems:
This folder is always named "SysWOW64", no matter of the language of the operating system. This works for .NET Framework 1.1 or above.
我需要这样做,但我还需要能够作为管理员远程执行此操作,无论哪种情况,这似乎对我来说都非常有效:
I need to do this, but I also need to be able as an admin do it remotely, either case this seems to work quite nicely for me:
这是基于 Microsoft 代码的解决方案,位于 http://1code.codeplex.com/ SourceControl/changeset/view/39074#842775。 它使用扩展方法来轻松重用代码。
一些可能的用法如下所示:
This is a solution based on Microsoft's code at http://1code.codeplex.com/SourceControl/changeset/view/39074#842775. It uses extension methods for easy code reuse.
Some possible usage is shown below:
以下是 C# 中使用来自 此页面。
Here is the direct approach in C# using DllImport from this page.
我正在使用以下代码。 注意:它是为 AnyCPU 项目制作的。
I'm using the followin code. Note: It's made for an AnyCPU project.
我发现这是检查系统平台和进程的最佳方法:
第一个属性对于 64 位系统返回 true,对于 32 位系统返回 false。
第二个属性对于 64 位进程返回 true,对于 32 位进程返回 false。
需要这两个属性是因为您可以在 64 位系统上运行 32 位进程,因此您需要检查系统和进程。
I found this to be the best way to check for the platform of the system and the process:
The first property returns true for 64-bit system, and false for 32-bit.
The second property returns true for 64-bit process, and false for 32-bit.
The need for these two properties is because you can run 32-bit processes on 64-bit system, so you will need to check for both the system and the process.
一切都很好,但这也应该在
env
中工作:..
太简单了,也许;-)
All fine, but this should also work from
env
:..
Too easy, maybe ;-)
以下是Windows Management Instrumentation (WMI) 方法:
Here's a Windows Management Instrumentation (WMI) approach:
OSInfo.Bits
OSInfo.Bits