Search code examples
c#wmi

Correct way to get total number of logical cores from remote pc


Trying to get the correct total number of cores from a remote machine through wmi. The results aren't always correct. This seems to be a problem when there are multiple cpu's and it's only returning the count for 1 proc and not both.

            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
            {
            }
         }

Solution

  • This line

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

    is in error.

    You need to add together all the cores found, not just use the last entry.