栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过.NET / C#查找CPU内核数?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过.NET / C#查找CPU内核数?

您可以获得与处理器有关的多种不同信息:

  1. 物理处理器数量
  2. 核心数
  3. 逻辑处理器数。

这些都可以不同。对于具有2个启用双核超线程的处理器的计算机,则有2个物理处理器,4个内核和8个逻辑处理器。

逻辑处理器的数量可通过Environment类获得,但其他信息仅可通过WMI获得(并且可能必须安装一些修补程序或Service
Pack
才能在某些系统上获得它):

确保在项目中添加对System.Management.dll的引用。 在.NET Core中,该 引用
作为NuGet包可用(仅适用于Windows)。

物理处理器:

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()){    Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);}

核心数:

int coreCount = 0;foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get()){    coreCount += int.Parse(item["NumberOfCores"].ToString());}Console.WriteLine("Number Of Cores: {0}", coreCount);

逻辑处理器:

Console.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount);

要么

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()){    Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);}

Windows排除的处理器:

您还可以在 setupapi.dll中 使用Windows
API调用来发现已从Windows排除的处理器(例如,通过启动设置),并且使用上述方法无法检测到。下面的代码给出了存在的逻辑处理器的总数(我无法弄清楚如何区分物理处理器与逻辑处理器),包括已从Windows中排除的逻辑处理器:

static void Main(string[] args){    int deviceCount = 0;    IntPtr deviceList = IntPtr.Zero;    // GUID for processor classid    Guid processorGuid = new Guid("{50127dc3-0f36-415e-a6cc-4cb3be910b65}");    try    {        // get a list of all processor devices        deviceList = SetupDiGetClassDevs(ref processorGuid, "ACPI", IntPtr.Zero, (int)DIGCF.PRESENT);        // attempt to process each item in the list        for (int deviceNumber = 0; ; deviceNumber++)        { SP_DEVINFO_DATA deviceInfo = new SP_DEVINFO_DATA(); deviceInfo.cbSize = Marshal.SizeOf(deviceInfo); // attempt to read the device info from the list, if this fails, we're at the end of the list if (!SetupDiEnumDeviceInfo(deviceList, deviceNumber, ref deviceInfo)) {     deviceCount = deviceNumber;     break; }        }    }    finally    {        if (deviceList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceList); }    }    Console.WriteLine("Number of cores: {0}", deviceCount);}[Dllimport("setupapi.dll", SetLastError = true)]private static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,    [MarshalAs(UnmanagedType.LPStr)]String enumerator,    IntPtr hwndParent,    Int32 Flags);[Dllimport("setupapi.dll", SetLastError = true)]private static extern Int32 SetupDiDestroyDeviceInfoList(IntPtr DeviceInfoSet);[Dllimport("setupapi.dll", SetLastError = true)]private static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet,    Int32 MemberIndex,    ref SP_DEVINFO_DATA DeviceInterfaceData);[StructLayout(LayoutKind.Sequential)]private struct SP_DEVINFO_DATA{    public int cbSize;    public Guid ClassGuid;    public uint DevInst;    public IntPtr Reserved;}private enum DIGCF{    DEFAULT = 0x1,    PRESENT = 0x2,    ALLCLASSES = 0x4,    PROFILE = 0x8,    DEVICEINTERFACE = 0x10,}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号