OK so there are a few common ways to check at runtime if your code is running on a 64 bit OS but each method I have seen is not 100% reliable. For example, checking the size of IntPtr (4 = 32 bit, 8 = 64 bit) is not a reliable way to see if the OS is 64 bit because your process could be running in 32 bit emulation mode on a 64 bit OS (aka Wow64) and therefore IntPtr would be 4 even though the OS is 64 bit. Another method is to check the PROCESSOR_ARCHITECTURE environmental variable but as a user can change environmental variables if they are so inclined then I dont count this as a fool proof way of checking either.

So, with .NET 4.0 Microsoft introduced the Is64BitOperatingSystem property to provide an easy way to check - which is great for those of us that can use .NET 4.0 in a production environment but in reality at the moment that is not very many people. So I have looked at the way this property works internally and copied it, making a few changes, for people to use in any version of .NET from 2.0 upwards.

So here is the definition of the property that you would check in your code:
vb.net Code:
  1. ''' <summary>
  2. ''' Determines whether or not the operating system is 64 bit
  3. ''' </summary>
  4.     Public ReadOnly Property Is64BitOperatingSystem() As Boolean
  5.         Get
  6.             If IntPtr.Size = 8 Then
  7.                 Return True
  8.             Else
  9.                 Dim Is64 As Boolean = False
  10.                 If DoesWin32MethodExist("kernel32.dll", "IsWow64Process") Then
  11.                     IsWow64Process(Process.GetCurrentProcess.Handle, Is64)
  12.                 End If
  13.                 Return Is64
  14.             End If
  15.         End Get
  16.     End Property

and here are the Windows API definitions that you will need to add to make that code work:
vb.net Code:
  1. 'API Definitions
  2.  
  3.     ''' <summary>
  4.     ''' Determines if a process is running on a 64 bit Operating System but in 32 bit emulation mode (WOW64)
  5.     ''' </summary>
  6.     ''' <param name="hProcess">A handle to the process to check</param>
  7.     ''' <param name="Wow64Process">Output parameter. A boolean that will be set to True if the process is running in WOW64 mode</param>
  8.     <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="IsWow64Process")> _
  9.     Public Shared Function IsWow64Process(<System.Runtime.InteropServices.InAttribute()> ByVal hProcess As System.IntPtr, <System.Runtime.InteropServices.OutAttribute()> ByRef Wow64Process As Boolean) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
  10.     End Function
  11.  
  12.     ''' <summary>
  13.     ''' Determines if a method exists in the specified DLL
  14.     ''' </summary>
  15.     ''' <param name="moduleName">The DLL to look for the method in</param>
  16.     ''' <param name="methodName">The method to look for</param>
  17.     Public Shared Function DoesWin32MethodExist(ByVal moduleName As String, ByVal methodName As String) As Boolean
  18.         Dim moduleHandle As IntPtr = GetModuleHandle(moduleName)
  19.         If (moduleHandle = IntPtr.Zero) Then
  20.             Return False
  21.         End If
  22.         Return (GetProcAddress(moduleHandle, methodName) <> IntPtr.Zero)
  23.     End Function
  24.  
  25.     ''' <summary>
  26.     ''' Gets a handle to a specified DLL
  27.     ''' </summary>
  28.     ''' <param name="moduleName">The module to return a handle for</param>
  29.     <Runtime.ConstrainedExecution.ReliabilityContract(Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, Runtime.ConstrainedExecution.Cer.MayFail), DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  30.     Public Shared Function GetModuleHandle(ByVal moduleName As String) As IntPtr
  31.     End Function
  32.  
  33.     ''' <summary>
  34.     ''' Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL)
  35.     ''' </summary>
  36.     ''' <param name="hModule">A handle to the DLL to look for the method in</param>
  37.     ''' <param name="methodName">The method to look for</param>
  38.     <DllImport("kernel32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)> _
  39.     Public Shared Function GetProcAddress(ByVal hModule As IntPtr, ByVal methodName As String) As IntPtr
  40.     End Function

In case you are wondering, the reason for the code checking to see if the IsWow64Process method exists in kernel32.dll before calling it is because prior to XP SP2 this method did not exist.

Tested on Windows 7 (64 bit) and Windows XP (32 bit) and worked perfectly on both.

Of course any corrections or suggestions feel free to post them here Hope it helps someone out.

Chris