使用C#识别CPU体系结构类型

Ani*_*oel 19 c# architecture cpu identify

我想检查用户正在运行的CPU架构,是i386还是X64或AMD64.我想用C#做.我知道我可以尝试WMI或注册表.除了这两个之外还有其他方式吗?我的项目面向.NET 2.0!

Meh*_*ari 27

你也可以试试(只有在没有被操纵的情况下才有效):

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
Run Code Online (Sandbox Code Playgroud)

  • @romeok:这不应该用于检测机器的处理器架构.它可用于检测程序运行的体系结构.如果要检查操作系统是否为64位,则应使用其他方法,如WMI.然而,OP明确提到他想要别的东西. (4认同)
  • 注意:返回值取决于调用进程的编译方式(x86、x64、AnyCPU)!解决方案是传递第二个参数:System.Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”,EnvironmentVariableTarget.Machine)另请参阅http://stackoverflow.com/a/1739055/1468842 (3认同)
  • 这是 Windows 特定的,不适用于 macOS 或 Linux。 (2认同)

小智 23

我在这里得到的是检查32对64位操作系统.评分最高的答案是查看当前流程的设置.找不到答案后,我找到了以下设置.希望这对你有用.

bool is64 = System.Environment.Is64BitOperatingSystem
Run Code Online (Sandbox Code Playgroud)

  • 很抱歉重振旧线程,但作为对未来读者的建议:由于32位进程可以在64位操作系统上运行,因此`System.Environment.Is64BitProcess`在我的情况下更有用(设置`PATH `基于体系结构的"正确"`sqlite3.dll`的变量.在我的情况下,操作系统确实是64位,但由于另一个库,我不得不将我的应用程序编译为32位. (2认同)

cub*_*e45 12

我知道这个问题来自过去,但截至 2017 年,现在有一个简单的方法来了解当前进程的架构,在 .net 标准中:

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture
Run Code Online (Sandbox Code Playgroud)

返回的值是 X86、X64、ARM、ARM64 之一,并给出运行它的进程OSArchitecture的体系结构。而是返回已安装操作系统的体系结构。

链接到文档(虽然很没用......):

RuntimeInformation.ProcessArchitecture:https ://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation.processarchitecture ? view = netstandard-1.4

架构枚举:https : //docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.architecture?view=netstandard-1.4

  • 这不适用于在 ARM64 上运行的 x86 .NET 应用程序:/sf/ask/3811929831/网内 (3认同)
  • 还有 RuntimeInformation.OSArchitecture,从 .NET 7 开始,它将为在 ARM64 上模拟运行的 X86 应用程序返回正确的值。https://github.com/dotnet/docs/issues/30894 (3认同)

Sim*_*ier 10

这是一段似乎有用的代码(基于P/Invoke):

    public static ProcessorArchitecture GetProcessorArchitecture()
    {
        SYSTEM_INFO si = new SYSTEM_INFO();
        GetNativeSystemInfo(ref si);
        switch (si.wProcessorArchitecture)
        {
            case PROCESSOR_ARCHITECTURE_AMD64:
                return ProcessorArchitecture.Amd64;

            case PROCESSOR_ARCHITECTURE_IA64:
                return ProcessorArchitecture.IA64;

            case PROCESSOR_ARCHITECTURE_INTEL:
                return ProcessorArchitecture.X86;

            default:
                return ProcessorArchitecture.None; // that's weird :-)
        }
    }
Run Code Online (Sandbox Code Playgroud)

    [DllImport("kernel32.dll")]
    private static extern void GetNativeSystemInfo(ref SYSTEM_INFO lpSystemInfo);

    private const int PROCESSOR_ARCHITECTURE_AMD64 = 9;
    private const int PROCESSOR_ARCHITECTURE_IA64 = 6;
    private const int PROCESSOR_ARCHITECTURE_INTEL = 0;

    [StructLayout(LayoutKind.Sequential)]
    private struct SYSTEM_INFO
    {
        public short wProcessorArchitecture;
        public short wReserved;
        public int dwPageSize;
        public IntPtr lpMinimumApplicationAddress;
        public IntPtr lpMaximumApplicationAddress;
        public IntPtr dwActiveProcessorMask;
        public int dwNumberOfProcessors;
        public int dwProcessorType;
        public int dwAllocationGranularity;
        public short wProcessorLevel;
        public short wProcessorRevision;
    }
Run Code Online (Sandbox Code Playgroud)

请注意,此代码重用现有CLR的ProcessorArchitecture枚举,并支持.NET framework 2及更高版本.


Ant*_*lev 6

Win32_Processor WMI类将完成这项工作.使用MgmtClassGen.exe生成强类型包装器.

  • 这可能是问题的最佳答案. (2认同)

met*_*ngs 5

最后,解决C#中当前运行的CLR运行时的平台/处理器架构的最短技巧是:

PortableExecutableKinds peKind;
ImageFileMachine machine;
typeof(object).Module.GetPEKind(out peKind, out machine);
Run Code Online (Sandbox Code Playgroud)

这里Module.GetPEKind返回一个ImageFileMachine枚举,它存在于.NET v2之后:

public enum ImageFileMachine
{
    I386    = 0x014C,
    IA64    = 0x0200,
    AMD64   = 0x8664,
    ARM     = 0x01C4    // new in .NET 4.5
}
Run Code Online (Sandbox Code Playgroud)

为什么不使用new AssemblyName(fullName)typeof(object).Assembly.GetName()
那么HACK在ASP.NET MVC源代码中有这个注释(从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);
}
Run Code Online (Sandbox Code Playgroud)

看到他们为自己使用一些隐藏的技巧.遗憾的是,AssemblyName构造函数没有ProcessorArchitecture适当地设置字段,它只None适用于任何新的AssemblyName.

因此,对于未来的读者,我建议您使用带有ImageFileMachine的丑陋的GetPEKind!

笔记:

  • 这将返回当前运行的运行时体系结构,而不是底层系统体系结
    也就是说,唯一的例外是I386运行时可能在AMD64系统上运行.
  • 在mono/ubuntu 14.04/AMD64和.NET/Win7/I386上测试.


ken*_*uno 5

这个怎么样?

switch (typeof(string).Assembly.GetName().ProcessorArchitecture) {
    case System.Reflection.ProcessorArchitecture.X86:
        // '$(Platform)' == 'x86'
        break;
    case System.Reflection.ProcessorArchitecture.Amd64:
        // '$(Platform)' == 'x64'
        break;
    case System.Reflection.ProcessorArchitecture.MSIL:
        // MacBook Pro with M1 Pro (from comment)
        break;
    case System.Reflection.ProcessorArchitecture.None:
        // ASP.NET Core Blazor WebAssembly
        break;
    case System.Reflection.ProcessorArchitecture.Arm:
        // Unknown
        break;
}
Run Code Online (Sandbox Code Playgroud)

case *.Arm:尚未测试。

  • 这会在我的配备 M1 Pro 的 MacBook Pro 上返回“ProcessorArchitecture.MSIL”。 (2认同)