Visual Studio一直说我有一个错误,我没有正确的“使用”,但我确实如此

本文关键字:使用 一直 Studio 有一个 错误 Visual | 更新日期: 2023-09-27 18:35:46

我有using System.Management;

但是,我不断收到此错误:

类型或命名空间名称"ManagementClass"在命名空间"System.Management"中不存在(是否缺少程序集引用?

我两次收到该错误。

我也收到此错误:

类型或命名空间名称"ManagementObjectCollection"在命名空间"System.Management"中不存在(是否缺少程序集引用?

为什么会这样?

如果有帮助,这是我的代码(完全取自StackOverflow,但仍然是我正在使用的代码)

private string identifier(string wmiClass, string wmiProperty)
    //Return a hardware identifier
    {
        string result = "";
        System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
        System.Management.ManagementObjectCollection moc = mc.GetInstances();
        foreach (System.Management.ManagementObject mo in moc)
        {
            //Only get the first one
            if (result == "")
            {
                try
                {
                    result = mo[wmiProperty].ToString();
                    break;
                }
                catch
                {
                }
            }
        }
        return result;
    }
    private void getButton_Click(object sender, EventArgs e)
    {
        string modelNo = identifier("Win32_DiskDrive", "Model");
        string manufatureID = identifier("Win32_DiskDrive", "Manufacturer");
        string signature = identifier("Win32_DiskDrive", "Signature");
        string totalHeads = identifier("Win32_DiskDrive", "TotalHeads");
    }

Visual Studio一直说我有一个错误,我没有正确的“使用”,但我确实如此

问题是 DLL 未在 Solution/{PROJECT}/References 下引用。右键单击References并选择"添加程序集"(或其他任何内容),然后转到 .NET 选项卡并找到System.Management .

仅仅因为存在 using 语句并不意味着引用了 DLL。

一个很好的指南在MSDN上结束了。