-1

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.

FiddlingAway
  • 1,598
  • 3
  • 14
  • 30
  • 1
    What is the **exact** error message you're getting from the compiler? What happens if you press `F12` when your caret is in `new ManagementObjectSearcher` ? (does the displayed metadata include the `Get` method?) – Dai Jan 26 '19 at 10:49
  • 1
    Possible solutions; https://stackoverflow.com/questions/4084402/get-hard-disk-serial-number https://stackoverflow.com/questions/20734683/how-to-get-the-hard-drive-serial-number https://stackoverflow.com/questions/4084402/get-hard-disk-serial-number – arunes Jan 26 '19 at 10:50
  • @Dai Updated the question with the exact error displayed. – FiddlingAway Jan 26 '19 at 10:55
  • @arunes Thanks, but the logic itself is not the issue here. Just using the `Get()` to retrieve query info. That keeps failing for some reason. – FiddlingAway Jan 26 '19 at 11:00
  • 1
    @FiddlingAway What is the file-name path and assembly version displayed in the top-line comment when you view the F12 definition? – Dai Jan 26 '19 at 11:07
  • 1
    I've just looked and looks like Get() method is there. Could be about the .Net Framework version. Which .Net Framework version is your project? – arunes Jan 26 '19 at 11:08
  • 3
    `namespace myProgram { internal class ManagementObjectSearcher }`? You have an override to the `ManagementObjectSearcher` in **your** namespace. You should see `namespace System.Management` as the class Namespace. Also, the access modifier would not be `internal` but `public`. Not directly related: I would ditch the 4.6.0 Framework. Install 4.7.1+, possibly. – Jimi Jan 26 '19 at 13:12
  • Try adding `using Microsoft.Win32` – preciousbetine Jan 26 '19 at 15:31
  • @Jimi Saw it, and I have no idea how it got there. The only thing which comes to mind is that I'd actually accepted the suggested fix. After removing it from the project, everything ran smoothly. – FiddlingAway Jan 28 '19 at 17:51

3 Answers3

1

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"

Kristóf Tóth
  • 791
  • 4
  • 19
1

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.

FiddlingAway
  • 1,598
  • 3
  • 14
  • 30
  • Likely it was "create new type" instead of "add using statement". An easy mistake, I've fallen victim of it numerous times :) – Alejandro Jan 28 '19 at 17:57
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 https://www.nuget.org/packages/System.Management/

ljnath
  • 1
  • 6