[英]How can I determine the "bit-ness" under which my C# application runs?
A .NET dll can be run as both 32 bit and 64 bit on a machine with an x64 processor. .NET dll 可以在带有 x64 处理器的机器上作为 32 位和 64 位运行。 I need to determine at runtime what bitness my application is running under.
我需要在运行时确定我的应用程序正在运行的位数。
Currently I've been doing something like System.IntPtr.Size == 8
, but that seems like an ugly hack.目前我一直在做类似
System.IntPtr.Size == 8
事情,但这似乎是一个丑陋的黑客。 Is there a more "correct" way of determining this?有没有更“正确”的方法来确定这一点?
In .NET 4 and beyond, including .NET Core, the System.Environment
class has two static properties: Is64BitOperatingSystem
and Is64BitProcess
.在 .NET 4 及更高版本(包括 .NET Core)中,
System.Environment
类具有两个静态属性: Is64BitOperatingSystem
和Is64BitProcess
。 In earlier .NET versions you need to use the IntPtr
size approach.在早期的 .NET 版本中,您需要使用
IntPtr
大小方法。
Pre .NET 4 it was suggested to use the size of an IntPtr
(4 for 32 bit and 8 for 64 bit).在 .NET 4 之前,建议使用
IntPtr
的大小(32 位为 4,64 位为 8)。 However, this doesn't give you the bitness of the machine - it gives you the bitness of the CLR that is being used.但是,这并没有为您提供机器的位数 -它为您提供了正在使用的 CLR 的位数。
That is an important difference if you are running inside a 32 bit process, such as application add-ins.如果您在 32 位进程(例如应用程序加载项)中运行,这是一个重要的区别。 I've got a blog post about finding the machines bitness based on WMI:
我有一篇关于基于 WMI 查找机器位数的博客文章:
http://adamhouldsworth.blogspot.com/2010/03/64bit-registry-from-32bit-application.html http://adamhouldsworth.blogspot.com/2010/03/64bit-registry-from-32bit-application.html
Note however, that I'm still unclear if this will truely represent the current OS bitness (as it's using the processor).但是请注意,我仍然不清楚这是否能真正代表当前的操作系统位数(因为它使用的是处理器)。
For the vast majority of situations, under normal compilation (AnyCPU) running your own app, IntPtr
will suffice.对于绝大多数情况,在运行您自己的应用程序的正常编译 (AnyCPU) 下,
IntPtr
就足够了。
In .NET 4, as others have said, there is now Environment.Is64BitProcess
and Environment.Is64BitOperatingSystem
.在 .NET 4 中,正如其他人所说,现在有
Environment.Is64BitProcess
和Environment.Is64BitOperatingSystem
。
In .Net 4.0 you can use在 .Net 4.0 中你可以使用
Environment.Is64BitProcess and
Environment.Is64BitOperatingSystem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.