从远程PC获取逻辑核心总数的正确方法

时间:2013-10-22 14:59:00

标签: c# wmi

尝试通过wmi从远程计算机获取正确的核心总数。结果并不总是正确的。当有多个cpu时,这似乎是一个问题,并且它只返回1个proc的计数,而不是两个。

            query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");
        searcher = new ManagementObjectSearcher(scope, query);

        queryCollection = searcher.Get();
        foreach (ManagementObject obj in queryCollection)
        {
            string manufacturer = obj["Manufacturer"].ToString().ToLower().Replace(',', ' ');
            row["Device Name"] = obj["name"].ToString().Replace(',', ' ');
            row["Manufacturer"] = manufacturer;
            row["Model"] = obj["Model"].ToString().Replace(',', ' ');
            row["Cores"] = obj["NumberOfProcessors"].ToString();

            if (manufacturer.Contains("microsoft corporation") || manufacturer.Contains("vmware") || manufacturer.Contains("virtualbox"))
            {
                row["Virtual"] = true;
            }
        }

        query = new ObjectQuery("SELECT * FROM Win32_Processor");
        searcher = new ManagementObjectSearcher(scope, query);

        queryCollection = searcher.Get();
        foreach (ManagementObject obj in queryCollection)
        {
            try
            {
                //2008 + only
                row["Cores"] = obj["NumberOfLogicalProcessors"].ToString();
            }
            catch
            {
            }
         }

1 个答案:

答案 0 :(得分:2)

这一行

row["Cores"] = obj["NumberOfLogicalProcessors"].ToString();

错误。

您需要将找到的所有核心加在一起,而不仅仅是使用最后一个条目。