【发布时间】:2010-09-20 22:23:46
【问题描述】:
如何判断我的应用程序(在 Visual Studio 2008 中编译为 Any CPU)是作为 32 位还是 64 位应用程序运行的?
【问题讨论】:
如何判断我的应用程序(在 Visual Studio 2008 中编译为 Any CPU)是作为 32 位还是 64 位应用程序运行的?
【问题讨论】:
【讨论】:
if (IntPtr.Size == 8)
{
// 64 bit machine
}
else if (IntPtr.Size == 4)
{
// 32 bit machine
}
【讨论】:
我从Martijn Boven 找到了这个代码:
public static bool Is64BitMode() {
return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
}
【讨论】:
来自 Microsoft All-In-One Code Framework 的代码示例可以回答您的问题:
Detect the process running platform in C# (CSPlatformDetector)
CSPlatformDetector 代码示例演示了以下任务 平台检测相关:
- 检测当前操作系统的名称。 (例如“Microsoft Windows 7 Enterprise”)
- 检测当前操作系统的版本。 (例如“Microsoft Windows NT 6.1.7600.0”)
- 确定当前操作系统是否为 64 位操作系统。
- 判断当前进程是否为64位进程。
- 确定系统上运行的任意进程是否为 64 位。
如果只想判断当前运行的进程是否是64位的 过程中,您可以使用 .NET 中的新属性 Environment.Is64BitProcess 框架 4.
如果要检测系统上是否运行了任意应用程序
是64位进程,需要判断OS位数,如果是64位,
使用目标进程句柄调用IsWow64Process():
static bool Is64BitProcess(IntPtr hProcess)
{
bool flag = false;
if (Environment.Is64BitOperatingSystem)
{
// On 64-bit OS, if a process is not running under Wow64 mode,
// the process must be a 64-bit process.
flag = !(NativeMethods.IsWow64Process(hProcess, out flag) && flag);
}
return flag;
}
【讨论】:
在 .Net Standard 中,您可以使用 System.Runtime.InteropServices.RuntimeInformation.OSArchitecture
【讨论】: