Managementobjectsearcher Does Not Contain a Definition for Get

I'm trying to get this simple piece of code to work.

    public void GetHDDSerial()
    {
        var hdd = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index = '0'")
            .Get()
            .Cast<ManagementObject>()
            .First();
        MessageBox.Show(hdd["Model"].ToString());
    }

using System.Management is present, and I've also made a reference to the assembly (Visual Studio > Project > Add Reference > System.Management).

The error I'm getting is that the Get() method is not defined. Specifically:

Error CS1061 'ManagementObjectSearcher' does not contain a definition for 'Get' and no extension method 'Get' accepting a first argument of type 'ManagementObjectSearcher' could be found (are you missing a using directive or an assembly reference?)

How come? I thought that the getters and setters were predefined. Do I need to reference anything else?

EDIT: Going through the ManagementObjectSearcher, and listing all the methods that are actually there, I get these methods: ToString, Equals, GetHashCode, GetType.

EDIT #2: Going to the definition (F12, or right-clicking), I get this:

namespace myProgram
{
    internal class ManagementObjectSearcher
    {
        private string v;

        public ManagementObjectSearcher(string v)
        {
            this.v = v;
        }
    }
}

.NET version is 4.6.01055, and I'm using Visual Studio 2015 Enterprise.

9

3 Answers

This code works for me and properly lists my primary drive. I added the following usings and added references to System.Management and System.Management.Instrumentation. Should be working for you to with .NET 4.6.1.

using System;
using System.Linq;
using System.Management;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var hdd = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Index = '0'")
            .Get()
            .Cast<ManagementObject>()
            .First();
            Console.WriteLine(hdd["Model"].ToString());

            Console.Read();

        }
    }
}

sample output: "Samsung SSD 840 EVO 250GB"

Figured out what the issue was. I must've clicked and accepted one of the suggested fixes without realizing, which created an override. Apologies for wasting everyone's time.

1

Encountered the same issue while build a .NET library with .NET 4.7.2 Resolved the problem by installing the System.Management package via nuget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest