C# 命名空间“System.Management”中不存在“ManagementClass”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1798152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:56:08  来源:igfitidea点击:

'ManagementClass' does not exist in the namespace 'System.Management'

c#asp.netmacosmac-address

提问by Luca Romagnoli

Hi i'm using this method for get the mac address

嗨,我正在使用这种方法来获取 mac 地址

public string GetMACAddress()
{
    System.Management.ManagementClass mc = default(System.Management.ManagementClass);
    ManagementObject mo = default(ManagementObject);
    mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

    ManagementObjectCollection moc = mc.GetInstances();
    foreach (var mo in moc)
    {
        if (mo.Item("IPEnabled") == true)
        {
            return mo.Item("MacAddress").ToString();
        }else
            return null;

    }

} 

but i receive this error

但我收到这个错误

Compiler Error Message: CS0234: The type or namespace name 'ManagementClass' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

What i have to do for fix it?

我该怎么做才能修复它?

thanks

谢谢

采纳答案by MoominTroll

You need to add a referenceto System.Management in your project.

您需要在项目中添加对 System.Management的引用

回答by Mongus Pong

Have you got the System.Management assembly referenced?

你有没有引用 System.Management 程序集?

回答by Maxim Zaslavsky

I think the issue here is that you don't have a proper "pointer" to the System.Management assembly. You must:

我认为这里的问题是您没有指向 System.Management 程序集的正确“指针”。你必须:

  • Use a using statement- in some of your code, you didn't prefix classes w/ System.Management, so you should either include:
  • 使用 using 语句- 在您的某些代码中,您没有使用 System.Management 为类添加前缀,因此您应该包括:

a.)

一种。)

using System.Management

or

或者

b.) a using block

b.) 一个 using 块

using(System.Management) 
{  
//your code goes here
}

Next, you need to have a real assembly reference. To do this in Visual Studio, right click on your project (or on References) in the Solution Explorer window and click Add Reference. After the list tabulates, find System.Management.dll in the .NET tab. Once you add it, it should work!

接下来,您需要有一个真正的程序集参考。要在 Visual Studio 中执行此操作,请在“解决方案资源管理器”窗口中右键单击您的项目(或“引用”),然后单击“添加引用”。在列表列表后,在 .NET 选项卡中找到 System.Management.dll。添加后,它应该可以工作!

回答by Ajay Tyagi

In the solution explorer:

在解决方案资源管理器中:

  1. add reference
  2. find and add system.management in .NET class
  1. 添加参考
  2. 在 .NET 类中查找并添加 system.management

that's it

就是这样

回答by Khawaja Asim

Please first make sure you add the Library Systems.Managementin to your project references.

请首先确保将库添加Systems.Management到您的项目引用中。

Then just use that referred assembly by including in your class.

然后只需通过包含在您的类中来使用该引用的程序集。

using System.Windows;