简体   繁体   中英

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.

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.

bool x86 = IntPtr.Size == 4;

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.

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM