In C #, how can I find out programmatically if the operating system is x64 or x86

In C #, how can I find out programmatically if the operating system is x64 or x86

I found this API method on the Internet, but it does not 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.

+4
source share
5 answers

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

And that's how he impemented

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

Use a reflector or similar to see how it works.

+8
source

bool x86 = IntPtr.Size == 4;

+2
source

If you create against AnyCPU and run it on a 64-bit system, it will work on the 64-bit version of the framework. On a 32-bit system, it will work on a 32-bit version of the framework. You can take advantage of this by simply checking the IntPtr.Size property. If Size = 4, you work on 32-bit, Size = 8, you work on 64-bit.

+1
source

Look at this:

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

I think you are looking for System.Environment.OSVersion

0
source

Below is this answer , so don't forget me for it :)

 if (8 == IntPtr.Size || (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")))) { // x64 } else { // x86 } 
0
source

All Articles