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.
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. 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"
使用 .NET 4.7.2 构建 .NET 库时遇到同样的问题 通过 nuget https://www.nuget.org/packages/System.Management/安装 System.Management 包解决了该问题
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.