Homam
Homam

Reputation: 23841

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

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.

Upvotes: 4

Views: 2575

Answers (5)

Jesper Palm
Jesper Palm

Reputation: 7238

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.

Upvotes: 8

Niels van der Rest
Niels van der Rest

Reputation: 32184

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
}

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61589

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.

Upvotes: 1

thelost
thelost

Reputation: 6694

bool x86 = IntPtr.Size == 4;

Upvotes: 2

Burt
Burt

Reputation: 7758

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

Upvotes: 0

Related Questions