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 资料的方法
You could also try (only works if it's not manipulated):
Finally the shortest trick to resolve the platform/processor architecture for the current running CLR runtime in C# is:
Here Module.GetPEKind returns an ImageFileMachine enumeration, which exists since .NET v2:
Why not use
new AssemblyName(fullName)
ortypeof(object).Assembly.GetName()
?Well there is this
HACK
comment in ASP.NET MVC source code (since 1.0):See they use some hidden tricks for themselves. Sadly, the
AssemblyName
constructor doesn't set theProcessorArchitecture
field appropriately, it's justNone
for whatever new AssemblyName.So for future readers, let me recommend you using that ugly GetPEKind with ImageFileMachine!
Notes:
That said, the only exception is that an I386 runtime may run on an AMD64 system.
Depending on why you want to know, you might find that checking the size of the IntPtr structure is the easiest way.
Win32_Processor WMI Class will do the job. Use MgmtClassGen.exe to generate strongly-typed wrappers.
Here is a piece of code that seems to work (based on P/Invoke):
with
Note this code reuses the existing CLR's ProcessorArchitecture enum, and supports .NET framework 2 and higher.
What led me here is checking for a 32 vs 64 bit OS. the highest rated answer is looking at the setting for the Current process. After not finding an answer I found the following setting. Hope this works for you.