2020年1月9日 星期四

[研究][C#] 取得 CPU 實體數、核心數、邏輯數

[研究][C#] 取得 CPU 實體數、Core核心數、邏輯數

2020-01-09

using System;
// 參考要加入 System.Management,然後才能用 System.Management.ManagementObjectSearcher

namespace CPUCoreCount
{
    class Program
    {
        static void Main(string[] args)
        {
            // Physical CPU Count
            foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
            {
                Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
            }

            // Core count

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

            //Logical CPU Count

            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"]);
            }
            Console.ReadLine();
        }
    }
}


(完)

沒有留言:

張貼留言