how2: detect 32 or 64 bit operating system
Saturday, October 13 2007
Just the other day, I wanted to detect if an application is running on 32 or 64 bit (x86 versus x64) Windows operating system so I could display it in a label control. I thought to myself "ok that should be easy with a registry key or a file version of a system file," but I didn't know a definitive source so off I go to the wild, wild web. A few searches [1, 2, 3] yielded no useful results for doing this programmatically [and yes, I tried Google too ;-)].
So I turned to my brother who has been working with operating system deployments in our data centers for the past 10 years [probably what I should have done in the first place]. Just as I thought, there is a registry key that gives you this information. Since I couldn't find it easily online, I'll add it here.
HKLM\System\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE
Adding this to a .NET C# application is only a few lines of code (using Microsoft.Win32 namespace):
private string GetProcessorArchitecture(){RegistryKey environmentKey = Registry.LocalMachine;// points to HKLM hiveenvironmentKey = environmentKey.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", false);string strEnvironment = environmentKey.GetValue("PROCESSOR_ARCHITECTURE").ToString();return strEnvironment;}
So there you go...how to determine whether the architecture of the operating system is 32-bit or 64-bit.
~tod

581 comment(s)
This should also work:System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Al- Cool, I didn't even think of checking the environment variables.
Actually, if I may suggest, neither is a perfect solution because then you may have to deal with parsing a vendor-specific Processor Architecture string.For example, on my 64bit HP 4400 machine, both approaches return the string "AMD64".Perhaps a better approach:ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT AddressWidth FROM Win32_Processor");ManagementObjectCollection collection = searcher.Get();bool is64Bit = (UInt16)(obj(0)["AddressWidth"]) == (UInt16)64;Of course, that foreach loop isn't perfect, but you get the idea.Regards,\Steven
Oops, right.There is no foreach loop there!\S
Steven- Yep, since my method returns a string you would still have to parse it for evaluation before proceeding. Hold on...I think I see a foreach loop there...oh wait, nope. :)