Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Cant get ManagmentObjectSearcher from System.Managment

Tags:

c#

Im trying to get hardware info from PC using ManagmentObjectSearcher (CPU, GPU etc).

Its pretty clear, that this class is included in System.Management (like is said in MSDN Site).

Could you tell me, how to get this object? I give a reference to this using statement.

The error that i get is:

CS0246 C# The type or namespace name could not be found (are you missing a using directive or an assembly reference?)

If it will help, my code:

using System.Management; //no error here

 namespace KMM_HighPerformance.Models
{
    class GetHardwareInfo
    {

        static public void GetCPU()
        {
            ManagmentObjectSearcher m = new ManagmentObjectSearcher(); //i get red line here
        }

    }
}

Thanks for any advices!


2 Answers

Change typo "ManagmentObjectSearcher" to "ManagementObjectSearcher" .

Try this if you want get processor ID:

using System.Management; 

 namespace KMM_HighPerformance.Models
 {
    class GetHardwareInfo
    {

        static public void GetCPU()
        {
            var mbs = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
            var mbsList = mbs.Get();

            foreach (ManagementObject mo in mbsList)
            {
               var cpuid = mo["ProcessorID"].ToString();

            }
        }

    }
}
like image 180
Hossein Avatar answered Nov 19 '25 08:11

Hossein


Under the project name right click on references - > Add reference in the newly opened windows under assemblies search for System.Management just tick System.Management one and press okay

enter image description here

like image 44
styx Avatar answered Nov 19 '25 08:11

styx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!