问题
I created a C# .NET console application that can run in Windows 10 x86, x64 and ARM64 (via emulator layer).
I would like to know how to detect that the application is running in those platforms. I know how to detect x86 and x64, but how to detect that the app is running inside ARM64?
This is a snapshot of Visual Studio running into my ARM64 System. You can see that it's detected as X86
回答1:
You will be able to detect the processor architecture by using
System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture
Which will then return an Architecure
enum: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=netstandard-2.0
回答2:
OK, this code works:
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
);
}
(via Calling IsWowProcess2 from C# .NET (P/Invoke))
来源:https://stackoverflow.com/questions/54456140/how-to-detect-were-running-under-the-arm64-version-of-windows-10-in-net