tags:

views:

128

answers:

6

Hi All,

In C#, how can I know programmatically if the Operating system is x64 or x86

I found this API method on the internet, but it doesn't work

[DllImport("kernel32.dll")]
public static extern bool IsWow64Process(System.IntPtr hProcess, out bool lpSystemInfo);

public static bool IsWow64Process1
{
   get
   {
       bool retVal = false;
       IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, out retVal);
       return retVal;
   }
}

Thanks in advance.

A: 

Have a look at this:

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

I think you are looking for System.Environment.OSVersion

Burt
That doesn’t seem to answer the question whether it’s 32-bit or 64-bit.
Timwi
the Environment.Is64BitProcess Property is supported only in .Net 4.0,and I'm using 3.5
Homam
+2  A: 

bool x86 = IntPtr.Size == 4;

thelost
this might not be correct - http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net
obelix
I've .Net 3.5, it seems working well, what do you think?
Homam
A: 

Here are numerous ways...

http://rongchaua.net/blog/c-how-to-determine-processor-64-bit-or-32-bit/

hkon
+1  A: 

If you build against AnyCPU, and you run on a 64-bit system, it will run on the 64-bit version of the framework. On a 32-bit system, it will run on the 32-bit version of the framework. You can use this to its advantage by simply checking the IntPtr.Size property. If the Size = 4, you are running on 32-bit, Size = 8, you are running on 64-bit.

Matthew Abbott
A: 

The following is from this answer, so don't upvote me for it :)

if (8 == IntPtr.Size
    || (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
    // x64
}
else
{
    // x86
}
Niels van der Rest
+5  A: 

In .NET 4.0 you can use the new Environment.Is64BitOperatingSystem property.

And this is how it's impemented

public static bool Is64BitOperatingSystem
{
    [SecuritySafeCritical]
    get
    {
        bool flag;
        return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag)) && flag);
    }
}

Use reflector or similar to see exactly how it works.

Jesper Palm
Yeah, they screwed that up pretty badly.
Hans Passant