如何在 .NET 中检测我们 运行 在 Windows 10 的 ARM64 版本下?

How to detect we're running under the ARM64 version of Windows 10 in .NET?

我创建了一个 C# .NET 控制台应用程序,它可以 Windows 10 x86、x64 和 ARM64(通过模拟器层)运行。

我想知道如何检测应用程序是否在这些平台上 运行ning。我知道如何检测 x86 和 x64,但是如何检测该应用 运行在 ARM64 中?

这是 Visual Studio 运行 进入我的 ARM64 系统的快照。你可以看到它被检测为 X86

您将能够使用

检测处理器架构
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture

然后 return 一个 Architecure 枚举:https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=netstandard-2.0

好的,这段代码有效:

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
    );
}

(通过 Calling IsWowProcess2 from C# .NET (P/Invoke)