75

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Redwood
  • 66,744
  • 41
  • 126
  • 187

5 Answers5

164

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

Environment.Is64BitProcess

Reference: Environment.Is64BitProcess Property (MSDN)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam
  • 3,181
  • 3
  • 17
  • 3
  • 4
    Thanks for posting the answer, that's great to know. I'm not going to change the current accepted answer because this question was originally about .NET 3.5 but I would encourage people to up vote your answer as well. – Redwood Aug 11 '10 at 20:26
74
if (IntPtr.Size == 8) 
{
    // 64 bit machine
} 
else if (IntPtr.Size == 4) 
{
    // 32 bit machine
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Perica Zivkovic
  • 2,610
  • 24
  • 32
5

I found this code from Martijn Boven that does the trick:

public static bool Is64BitMode() {
    return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
}
Redwood
  • 66,744
  • 41
  • 126
  • 187
5

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

Detect the process running platform in 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.

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

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:

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;
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
Scott Ge
  • 91
  • 1
  • 6
1

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

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64