简体   繁体   English

ManagementObjectSearcher 不包含 Get 的定义

[英]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). using System.Management存在,我还引用了程序集(Visual Studio > 项目 > 添加引用 > System.Management)。

The error I'm getting is that the Get() method is not defined.我得到的错误是未定义 Get() 方法。 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?)错误 CS1061“ManagementObjectSearcher”不包含“Get”的定义,并且找不到接受“ManagementObjectSearcher”类型的第一个参数的扩展方法“Get”(您是否缺少 using 指令或程序集引用?)

How come?怎么来的? I thought that the getters and setters were predefined.我认为 getter 和 setter 是预定义的。 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 .编辑:通过 ManagementObjectSearcher,并列出所有实际存在的方法,我得到这些方法: ToStringEqualsGetHashCodeGetType

EDIT #2 : Going to the definition (F12, or right-clicking), I get this:编辑#2 :转到定义(F12,或右键单击),我得到这个:

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. .NET 版本是 4.6.01055,我使用的是 Visual Studio 2015 Enterprise。

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.为浪费大家的时间而道歉。

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.我添加了以下使用并添加了对 System.Management 和 System.Management.Instrumentation 的引用。 Should be working for you to with .NET 4.6.1.应该适合您使用 .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"示例输出:“三星 SSD 840 EVO 250GB”

使用 .NET 4.7.2 构建 .NET 库时遇到同样的问题 通过 nuget https://www.nuget.org/packages/System.Management/安装 System.Management 包解决了该问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM