What is the most reliable way to discover Windows OS Architecture (x86, x64) on .NET 2.0

I am using Visual C # 2010 express and I need the most reliable way (at the click of a button) in the .NET 2.0 platform to determine if there are currently x86 or x64 windows in the message box .. so far I am using this code but I need to know if there is a more accurate way?

string target = @"C:\Windows\SysWow64"; { if (Directory.Exists(target)) { MessageBox.Show("x64"); } else { MessageBox.Show("x86"); } 
+1
source share
5 answers

In contrast, the simplest test is checking the size of IntPtr:

  if (IntPtr.Size == 8) { MessageBox.Show("x64"); } else { MessageBox.Show("x86"); } 

It is assumed that you create your EXE with the default Target Target platform in "Any CPU". Remember that this default value has been changed in VS2010.

+9
source

You can use the environment variable PROCESSOR_ARCHITECTURE ...

 System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") 

It will return one of: x86, AMD64, IA64.

You are probably only interested in the x86 and AMD64 values, IA64 is not very popular and is not supported by Microsoft in the future. This is for Itanium and Itanium 2 processors.

Another easy way is to look in the registry to see if SOFTWARE\Wow6432Node for HKLM or HKCU .

+7
source

I think the best way is to use: System.Environment.Is64BitOperatingSystem .

+6
source

If it was a while. I find this compatible with .NET 2.0, but I'm not quite sure. You are probably only interested in cases 0 and 9 (they are most common anyway).

 public static string GetCpuArch() { ManagementScope scope = new ManagementScope(); ObjectQuery query = new ObjectQuery("SELECT Architecture FROM Win32_Processor"); ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query); ManagementObjectCollection results = search.Get(); ManagementObjectCollection.ManagementObjectEnumerator e = results.GetEnumerator(); e.MoveNext(); ushort arch = (ushort)e.Current["Architecture"]; switch (arch) { case 0: return "x86"; case 1: return "MIPS"; case 2: return "Alpha"; case 3: return "PowerPC"; case 6: return "Itanium"; case 9: return "x64"; default: return "Unknown Architecture (WMI ID " + arch.ToString() + ")"; } } 
+4
source

The environment variable PROCESSOR_ARCHITECTURE contains the address width of the process that is being executed, which does not necessarily apply to the operating system or processors. A quick way to see this is by running the following command ...

 $Env:PROCESSOR_ARCHITECTURE 

... in 32-bit and 64-bit PowerShell sessions and output comparison.

So, if GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") returns "AMD64" , then you definitely have a 64-bit process, operating systems, and processors. If it returns "x86" then you will definitely have a 32-bit process, although you still won’t know whether the 32-bit or 64-bit operating system and processors are.

If you are really after the width of the address of the operating system, then .NET 4 offers the Environment. Property Is64BitOperatingSystem . You can also use WMI for any version of .NET to read the OSArchitecture property of the OSArchitecture class :

 static string GetOSArchitecture() { var query = new WqlObjectQuery("SELECT OSArchitecture FROM Win32_OperatingSystem"); using (var searcher = new ManagementObjectSearcher(query)) using (var results = searcher.Get()) using (var enumerator = results.GetEnumerator()) { enumerator.MoveNext(); return (string) enumerator.Current.GetPropertyValue("OSArchitecture"); } } 

... although, unfortunately, the OSArchitecture property OSArchitecture exists in Windows Vista / Server 2008 and later.

For all versions of Windows since 2000, you can try p / call the GetSystemInfo () function and checking the wProcessorArchitecture member of the wProcessorArchitecture structure.

+2
source

All Articles