The PROCESSOR_ARCHITECTURE
environment variable contains the address width of the running process, which is not necessarily that of the operating system or processors. A quick way to see this is by running the following command...
$Env:PROCESSOR_ARCHITECTURE
...in 32- and 64-bit PowerShell sessions and comparing the output.
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 definitely have a 32-bit process, though you still won't know if the operating system and processors are 32- or 64-bit.
If you're really after the address width of the operating system, then .NET 4 offers the Environment.Is64BitOperatingSystem property. You can also use WMI on any version of .NET to read the OSArchitecture
property of the Win32_OperatingSystem 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");
}
}
...though, unfortunately, the OSArchitecture
property only exists on Windows Vista/Server 2008 and above.
For all versions of Windows since 2000, you might try p/invoking the GetSystemInfo() function and checking the wProcessorArchitecture
member of the SYSTEM_INFO structure.