Friday, October 17, 2008

C# Code to check if the machine is 64-bit

I once had a requirement for the same problem. I figured out the solution but never made a point to blog it. But recently few others had the same issue and I had to think again how I did it. Anyway here is the simple method which tells you if you are running a 64-bit OS.

public bool Is64BitMachine()
return IntPtr.Size == 8;

The idea is to use size of the IntPtr to determine if it is 64-bit or 32-bit. In case of 32-bit machines, IntPtr size is 4 bytes.

1 comment:

Ivan said...

This doesn't detect if the machine is 64bit. It simply detects if the executable is running with the 64bit runtime. If you change your app to target x86 and run it, the size will report as 4 bytes.