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!
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- slurm: use a control node also for computing
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
This seems the simplest to me:
Here's what I did:
If you're on 64 bit architecture you'll have two program file env variables. If you're on x86, you'll only have the one.
Here is my way:
If the operating system is Linux, pinvoke the libc-syscall uname, where you will have the processor in the Machine-field.
If the OS is Windows, check if System.IntPtr.Size * 8 = 64, then it's 64 bit. If it isn't 64-Bit, you check if IsWow64Process exists, and if it exists, and the process is Wow64, then it's x86-64, else it's x86-32.
This one is reliable.
Checking the processor-architecture environment variables is not.
Code:
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/)
How about this?
However
case *.Arm:
isn't tested yet.Maybe this CodeProject article could help? It uses the ManagementObjectSearcher in the System.Management namespace to search for hardware info.