Using IntPtr
IntPtr used to keep native handles in dotNET. Because of its structure it is important to what size it is. This is a our trick, checking size of IntPtr. IntPtr will be 8 if application is running on a 64bit OS, otherwise in 32bit OS it will be 4.
C# Code
if(IntPtr.Size == 8)
{
}
else if(IntPtr.Size == 4)
{
}
VB.NET Code
If IntPtr.Size = 8 Then
ElseIf IntPtr.Size = 4 Then
End If
Other methods
Another way is checking for the existence of the "ProgramFiles(x86)" environment variable. This is not a recomended way but anyway you can use it!
public bool Is64bitOS
{
get { return (Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null); }
}
public string ProgramFilesX86
{
get
{
string programFiles = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
if (programFiles == null)
{
programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
}
return programFiles;
}
}
The Is64bitOS property will be true if application is ruunig on a 64bit OS.
Another way is GetNativeSystemInfo windows API. Because it is an API and needs PInvode it is not recommended but it is a reliable way.