C# 如何判断我的应用程序是作为 32 位还是 64 位应用程序运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/266082/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:31:11  来源:igfitidea点击:

How do I tell if my application is running as a 32-bit or 64-bit application?

c#64-bit32-bit

提问by Lawrence Johnston

How do I tell if my application (compiled in Visual Studio 2008 as Any CPU) is running as a 32-bit or 64-bit application?

如何判断我的应用程序(在 Visual Studio 2008 中编译为Any CPU)是作为 32 位应用程序还是 64 位应用程序运行?

采纳答案by Perica Zivkovic

if (IntPtr.Size == 8) 
{
    // 64 bit machine
} 
else if (IntPtr.Size == 4) 
{
    // 32 bit machine
}

回答by Lawrence Johnston

I found this code from Martijn Boventhat does the trick:

我从Martijn Boven找到了这段代码,它可以解决问题:

public static bool Is64BitMode() {
    return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
}

回答by Sam

If you're using .NET4.0, it's a one-liner for the current process:

如果您使用.NET4.0,则它是当前进程的单行:

Environment.Is64BitProcess

Reference: Environment.Is64BitProcess Property (MSDN)

参考:Environment.Is64BitProcess 属性(MSDN)

回答by Scott Ge

This code sample from Microsoft All-In-One Code Framework can answer your question:

来自 Microsoft All-In-One Code Framework 的此代码示例可以回答您的问题:

Detect the process running platform in C# (CSPlatformDetector)

C#中检测进程运行平台(CSPlatformDetector)

The CSPlatformDetector code sample demonstrates the following tasks related to platform detection:

  1. Detect the name of the current operating system. (e.g. "Microsoft Windows 7 Enterprise")
  2. Detect the version of the current operating system. (e.g. "Microsoft Windows NT 6.1.7600.0")
  3. Determine whether the current operating system is a 64-bit operating system.
  4. Determine whether the current process is a 64-bit process.
  5. Determine whether an arbitrary process running on the system is 64-bit.

CSPlatformDetector 代码示例演示了以下与平台检测相关的任务:

  1. 检测当前操作系统的名称。 (例如“Microsoft Windows 7 企业版”)
  2. 检测当前操作系统的版本。 (例如“Microsoft Windows NT 6.1.7600.0”)
  3. 判断当前操作系统是否为64位操作系统。
  4. 判断当前进程是否为64位进程。
  5. 确定系统上运行的任意进程是否为 64 位。

If you just want to determine whether the currently running process is a 64-bit process, you can use the Environment.Is64BitProcessproperty that is new in .NET Framework 4.

如果只想确定当前运行的进程是否为 64 位进程,可以使用.NET Framework 4 中新增的Environment.Is64BitProcess属性。

And if you want to detect whether an arbitrary application running on the system is a 64-bit process, you need to determine the OS bitness, and if it is 64-bit, call IsWow64Process()with the target process handle:

并且如果要检测系统上运行的任意应用程序是否为64位进程,则需要确定操作系统位数,如果是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;
}

回答by Owen Pauling

In .Net Standard you can use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture

在 .Net Standard 中,您可以使用 System.Runtime.InteropServices.RuntimeInformation.OSArchitecture