【问题标题】:'ManagementClass' does not exist in the namespace 'System.Management'命名空间“System.Management”中不存在“ManagementClass”
【发布时间】:2010-12-20 08:49:43
【问题描述】:

你好我正在使用这个方法来获取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;

    }

} 

但我收到此错误

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

我必须做些什么来修复它?

谢谢

【问题讨论】:

  • 阅读一步一步的方法,将System.Management的引用添加到您的项目中。

标签: c# asp.net macos mac-address


【解决方案1】:

你需要add a reference到你项目中的System.Management。

【讨论】:

  • 链接已损坏 :(
  • 更新了链接! :)
  • 它在 2017 年有效吗?我在参考弹出窗口中找不到 System.management。我检查了所有以 System.开头的库。但仍然有同样的错误。我需要在 Mac 上安装任何东西吗?
  • 不幸的是不确定Mac,但如果我在VS 2017中搜索System.Management,我会得到results
【解决方案2】:

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

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

using System.Windows;

【讨论】:

    【解决方案3】:

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

    1. 添加参考
    2. 在 .NET 类中查找并添加 system.management

    就是这样

    【讨论】:

      【解决方案4】:

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

      • 使用 using 语句 - 在您的某些代码中,您没有使用 System.Management 为类添加前缀,因此您应该包括:

      a.)

      using System.Management
      

      b.) 使用块

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

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

      【讨论】:

        【解决方案5】:

        您是否引用了 System.Management 程序集?

        【讨论】:

          猜你喜欢
          • 2011-05-17
          • 2014-04-09
          • 2014-09-09
          • 2011-10-19
          • 2019-06-26
          • 1970-01-01
          • 1970-01-01
          • 2019-03-01
          • 2010-10-21
          相关资源
          最近更新 更多