-->

识别使用C#的CPU体系结构类型识别使用C#的CPU体系结构类型(Identifying the C

2019-05-12 01:53发布

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

Answer 1:

您也可以尝试(仅当它没有操纵工程):

System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")


Answer 2:

是什么导致我在这里是检查32和64位操作系统。 收视率最高的答案是看设置当前进程 。 没有找到答案后,我发现了以下设置。 希望这对你的作品。

bool is64 = System.Environment.Is64BitOperatingSystem


Answer 3:

这里是一段代码,似乎工作(基于P /调用):

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

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

注意:此代码重用现有的CLR的ProcessorArchitecture用于枚举,并支持.NET框架2或更高。



Answer 4:

Win32_Processor WMI类将做的工作。 使用MgmtClassGen.exe生成强类型的包装。



Answer 5:

最后,最短的伎俩,以解决在C#当前运行的CLR运行时平台/处理器架构是:

PortableExecutableKinds peKind;
ImageFileMachine machine;
typeof(object).Module.GetPEKind(out peKind, out machine);

这里Module.GetPEKind返回ImageFileMachine枚举,因为.NET V2它存在:

public enum ImageFileMachine
{
    I386    = 0x014C,
    IA64    = 0x0200,
    AMD64   = 0x8664,
    ARM     = 0x01C4    // new in .NET 4.5
}

为什么不使用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);
}

看看他们使用一些隐藏的技巧为自己。 可悲的是, AssemblyName构造不设置ProcessorArchitecture适当的领域,它只是None为任何新的AssemblyName。

因此,对于未来的读者,让我推荐你使用那个丑陋的GetPEKind与ImageFileMachine!

笔记:

  • 这将返回当前正在运行的运行架构 ,而不是基本的系统架构!
    这就是说,唯一的例外是,一个I386运行时可以在AMD64系统上运行。
  • 测试在单/ Ubuntu的14.04 / AMD64和.NET / Win7的/ I386。


Answer 6:

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

System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture

返回的值是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



Answer 7:

也许这 CodeProject上的文章可以帮助? 它使用ManagementObjectSearcher在System.Management命名空间来搜索硬件信息。



Answer 8:

根据为什么你要知道,你可能会发现,检查的IntPtr结构的大小是最简单的方法。



Answer 9:

也许你可以要求用户?

只是在开玩笑,当然......我认为WMI是你会用什么为。 但也许还有一些其他的方式呢?

如果你去WMI然后LinqToWmi可以使用的。 我尝试过了一次,它似乎相当直截了当=) - > http://www.codeplex.com/linq2wmi



Answer 10:

我相信你应该避免重膨胀像WMI和LINQ ..,你就会有最终,以获得更多的信息,你走的,没有一个被臃肿的API和框架感到满意。

只需调用调用并提取CPUID信息的DLL。 C ++ / CLI或PInvoke的会做,并得到你所需要的供应商中的所有信息。 首先,你需要看的指令是否被支持(它是99%的时间)。

要获得快速启动和运行是检查wincpuid样品intel的网站,并从那里提取cpuid.h片。 只有2个供应商,一个是良好与存储器等待时间,而另一个不是(像天然VS托管代码)。 所以,你会在其他架构等有问题,用单(谁不BTW)。 至于64,你已经知道了,或只是让corflags(它的存在已经和杀灭与.NET分配客户的硬盘驱动器)..

( http://software.intel.com/en-us/articles/api-detects-ia-32-and-x64-platform-cpu-characteristics/ )



Answer 11:

这似乎是最简单的对我说:

System.Environment.Is64BitOperatingSystem


Answer 12:

这个怎么样?

switch (typeof(string).Assembly.GetName().ProcessorArchitecture) {
    case System.Reflection.ProcessorArchitecture.X86:
        break;
    case System.Reflection.ProcessorArchitecture.Amd64:
        break;
    case System.Reflection.ProcessorArchitecture.Arm:
        break;
}

然而case *.Arm:尚未被测试。



Answer 13:

这是我做的:

public static bool Isx86()
{
    return (Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%").Length == 0);
}

如果你在64位架构,您有两种程序文件ENV变量。 如果你在x86,你只有一个。



文章来源: Identifying the CPU architecture type using C#