Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

64-bit

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.

like image 612
Homam Avatar asked Dec 22 '22 00:12

Homam


1 Answers

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.

like image 102
Jesper Palm Avatar answered Apr 26 '23 23:04

Jesper Palm