简体   繁体   English

如何确定我的 C# 应用程序运行的“位”?

[英]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类具有两个静态属性: Is64BitOperatingSystemIs64BitProcess 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.Is64BitProcessEnvironment.Is64BitOperatingSystem

In .Net 4.0 you can use在 .Net 4.0 中你可以使用

Environment.Is64BitProcess and
Environment.Is64BitOperatingSystem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C#确定本地网络上的哪些IP地址是静态/动态的? - How can I use C# to determine which IP addresses on my local network are static/dynamic? 如何确定我的数据库中存在/填充了哪些属性 - 并创建排名 - c# - How can I determine which properties exist/are filled in my database - and create rank - c# 如何确定 C# 应用程序的空闲时间? - How do I determine idle time in my C# application? 如果我的 C# AnyCPU 应用程序以 32 位 (WOW64) 运行,我如何将其重新运行为 64 位? - How I can re-run my C# AnyCPU application as 64 bit, if it's running as 32 bit (WOW64)? 如何获得应用程序池在WCF方法内部运行的帐户的名称? - How can I get the name of the account the application pool runs under inside of my WCF method? 如何在C#中将应用程序编写为多线程应用程序? - How can I write my application as a multithreaded application in C#? 如何使用P / Invoke从64位C#应用程序中调用64位C ++ DLL? - How can I use P/Invoke to call a 64-bit C++ DLL from a 64-bit C# Application? 如何播放参考资料中的Flash电影? (C#) - How can I play a flashmovie which is in my references? (C#) 什么运行我的C#应用​​程序? - What runs my C# application? 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM