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
Finally the shortest trick to resolve the platform/processor architecture for the current running CLR runtime in C# is:
PortableExecutableKinds peKind;
ImageFileMachine machine;
typeof(object).Module.GetPEKind(out peKind, out machine);
Here Module.GetPEKind returns an ImageFileMachine enumeration, which exists since .NET v2:
public enum ImageFileMachine
{
I386 = 0x014C,
IA64 = 0x0200,
AMD64 = 0x8664,
ARM = 0x01C4 // new in .NET 4.5
}
Why not use new AssemblyName(fullName) or typeof(object).Assembly.GetName()?
Well there is this HACK comment in ASP.NET MVC source code (since 1.0):
private static string GetMvcVersionString() {
// DevDiv 216459:
// This code originally used Assembly.GetName(), but that requires FileIOPermission, which isn't granted in
// medium trust. However, Assembly.FullName *is* accessible in medium trust.
return new AssemblyName(typeof(MvcHttpHandler).Assembly.FullName).Version.ToString(2);
}
See they use some hidden tricks for themselves. Sadly, the AssemblyName constructor doesn't set the ProcessorArchitecture field appropriately, it's just None for whatever new AssemblyName.
So for future readers, let me recommend you using that ugly GetPEKind with ImageFileMachine!
Notes: