views:

2285

answers:

5

I want to check which CPU architecture is the user running, is it i386 or X64 or AMD64. I want to do it in C#. I know i can try WMI or Registry. Is there any other way apart from these two? My project targets .NET 2.0!

A: 

You could ask the user perhaps?

Just kidding of course... I think WMI is what you would use for that. But maybe there is some other way as well?

If you go for WMI then LinqToWmi could be of use. I tried it out once, and it seemed pretty straight forward =) -> http://www.codeplex.com/linq2wmi

Svish
+3  A: 

Win32_Processor WMI Class will do the job. Use MgmtClassGen.exe to generate strongly-typed wrappers.

Anton Gogolev
This is probably the best answer to the question.
Sky Sanders
+8  A: 

You could also try (only works if it's not manipulated):

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Mehrdad Afshari
what if it is not set?
Anirudh Goel
Not set goes to "manipulated" case (at least on Vista, it's set by default). I suggested it as an alternate way, not necessarily the best way.
Mehrdad Afshari
nice answer though.
Anirudh Goel
I am on AMD64 and Vista 64bit and looking at the variables in the console window I see AMD64 for PROCESSOR_ARCHITECTURE, BUT running my unit tests using ReSharper I see "x86" returned. Probably OK if the test runs in 32bit mode, BUT I use this result to decide how to launch another executable and select its 32bit version which refuses to start on 64bit os.
romeok
@romeok: This should NOT be used to detect the processor architecture of the machine. It may be used to detect the architecture the program is running on. If you want to check to see whether the OS is 64-bit you should use other methods like WMI. However the OP explicitly mentioned he wants something else.
Mehrdad Afshari
A: 

Maybe this CodeProject article could help? It uses the ManagementObjectSearcher in the System.Management namespace to search for hardware info.

Erik Hellström
A: 

I believe you should avoid heavy bloat like WMI and LINQ.. and you'll have to eventually, to get more info as you go along, none of which are satisfied by bloated apis and frameworks.

Just invoke a dll that calls and extracts CPUID info. C++/CLI or pinvoke would do and get all the info you need on the vendor. First you need to see whether the instruction is supported (99% of the time it is).

To get quickly up and running is to check the intel site for wincpuid sample and extract the piece from cpuid.h from there. There are only 2 vendors and one is good with memory latency and the other one isn't (like native vs managed code). So you'll have issues with Mono on other architectures etc (who doesn't btw). As for x64 you already know it or just get the corflags (its there already and killing your customer hard drive with .NET distribution )..

(http://software.intel.com/en-us/articles/api-detects-ia-32-and-x64-platform-cpu-characteristics/)

rama-jka toti