tags:

views:

1127

answers:

3

I'm trying to figure out how to tell if my application (compiled in VS2008 as Any CPU) is running as a 32 or 64 bit app.

+5  A: 

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

public static bool Is64BitMode() {
    return System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)) == 8;
}
Lawrence Johnston
It's likely more efficient to call IntPtr.Size instead of Marshal.SizeOf(typeof(IntPtr))
JaredPar
I'd love to give +1 to JaredPar's comment; that's how I would do it...
Marc Gravell
+11  A: 
if(IntPtr.Size == 8) 
{
// 64 bit machine
} 
else if(IntPtr.Size == 4) 
{
// 32 bit machine
}
Perica Zivkovic
+3  A: 

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

Environment.Is64BitProcess

http://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx

Sam
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.
Lawrence Johnston