从 .NET Standard 1.1 开始,dotnet 新增了 Architecure
枚举,用于表示处理器的架构(X86、X64、Arm或Arm64)。可参考微软的官方文档:
仅需一行代码即可获取:
Console.WriteLine("ProcessArchitecture:"); Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture);
在 Arm64 处理器的机器上,输出如下:
ProcessArchitecture: Arm64
在 X64 处理器的机器上,输出如下:
ProcessArchitecture: X64
如果需要在更低版本的 Framework 上获取 CPU 是否是 ARM64 架构,可以使用以下代码(仅支持 Windows 平台):
//https://stackoverflow.com/questions/54456140/how-to-detect-were-running-under-the-arm64-version-of-windows-10-in-net public static class ArchitectureInfo { public static bool IsArm64() { var handle = Process.GetCurrentProcess().Handle; IsWow64Process2(handle, out var processMachine, out var nativeMachine); return nativeMachine == 0xaa64; } [DllImport("kernel32.dll", SetLastError = true)] private static extern bool IsWow64Process2( IntPtr process, out ushort processMachine, out ushort nativeMachine ); }