C#错误-找不到命名空间ManagementClass,ManagementObject和ManagementObjectCollection [英] C# Error - Namespaces ManagementClass, ManagementObject, and ManagementObjectCollection could not be found

查看:101
本文介绍了C#错误-找不到命名空间ManagementClass,ManagementObject和ManagementObjectCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在导入C#类后,我得到了完整的错误列表.我搜索了该错误并获得了很多成功,但是他们都说只添加System.Management命名空间即可,但是却给出了这些错误.

So, I've got a full list of errors after importing a C# Class. I searched for the error and got a ton of hits however they all say to just add the System.Management namespace which is there and yet it gives these errors.

类似的问题.没有解决方案对我有用:是否缺少使用WMI ManagementObjectSearcher的指令或程序集引用?

Similar questions. No solution worked for me: Missing directive or assembly reference using WMI ManagementObjectSearcher?

"ManagementClass"在名称空间"System"中不存在.管理"

班级:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Net;
using System.Xml;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Threading;
using System.Management;
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.Security.Cryptography;

namespace PCID_Grabber
{

public class PCID
{
    public static string GetCPUId()
    {
        #region CPUid
        string cpuInfo = String.Empty;
        string temp = String.Empty;
        ManagementClass mc = new ManagementClass("Win32_Processor");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            if (cpuInfo == String.Empty)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
            }
        }
        return cpuInfo;
        #endregion
    }

    public static string GetMotherBoardID()
    {
        #region Mboard
        ManagementObjectCollection mbCol = new ManagementClass("Win32_BaseBoard").GetInstances();
        ManagementObjectCollection.ManagementObjectEnumerator mbEnum = mbCol.GetEnumerator();
        mbEnum.MoveNext();
        return ((ManagementObject)(mbEnum.Current)).Properties["SerialNumber"].Value.ToString();
        #endregion
    }

    public static string GetMacAddress()
    {
        #region MacAddress
        string macs = "";
        NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface ni in interfaces)
        {
            PhysicalAddress pa = ni.GetPhysicalAddress();
            macs += pa.ToString();
        }
        return macs;
        #endregion
    }
    public static string GetVolumeSerial()
    {
        #region HD serial
        string strDriveLetter = "";
        ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
        ManagementObjectCollection moc = mc.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            try
            {
                if ((UInt16)mo["MediaType"] == 29)
                {
                    String serial = mo["SerialNumber"].ToString().Trim();
                    if (!String.IsNullOrEmpty(serial))
                    {
                        strDriveLetter = (string)mo["SerialNumber"];
                        return strDriveLetter;
                    }
                }
            }
            catch { }
        }
        return strDriveLetter;
        #endregion
    }
    public static string GetGenericID()
    {
        #region UID
        string ID = GetCPUId()  + GetMotherBoardID()  + GetMacAddress()  + GetVolumeSerial();
        HMACSHA1 hmac = new HMACSHA1();
        hmac.Key = Encoding.ASCII.GetBytes(GetMotherBoardID());
        hmac.ComputeHash(Encoding.ASCII.GetBytes(ID));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hmac.Hash.Length; i++)
        {
            sb.Append(hmac.Hash[i].ToString("X2"));
        }
        return sb.ToString();
        #endregion
    }
}
}

错误列表:

Error   1   The type or namespace name 'ManagementClass' could not be found (are you missing a using directive or an assembly reference?)   C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  31  13  PCID Grabber
Error   2   The type or namespace name 'ManagementClass' could not be found (are you missing a using directive or an assembly reference?)   C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  31  38  PCID Grabber
Error   5   The type or namespace name 'ManagementClass' could not be found (are you missing a using directive or an assembly reference?)   C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  46  52  PCID Grabber
Error   8   The type or namespace name 'ManagementClass' could not be found (are you missing a using directive or an assembly reference?)   C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  69  13  PCID Grabber
Error   9   The type or namespace name 'ManagementClass' could not be found (are you missing a using directive or an assembly reference?)   C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  69  38  PCID Grabber
Error   7   The type or namespace name 'ManagementObject' could not be found (are you missing a using directive or an assembly reference?)  C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  49  22  PCID Grabber
Error   3   The type or namespace name 'ManagementObjectCollection' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  32  13  PCID Grabber
Error   4   The type or namespace name 'ManagementObjectCollection' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  46  13  PCID Grabber
Error   6   The type or namespace name 'ManagementObjectCollection' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  47  13  PCID Grabber
Error   10  The type or namespace name 'ManagementObjectCollection' could not be found (are you missing a using directive or an assembly reference?)    C:\Users\Keanu\workspace\PCID Grabber\PCID Grabber\pcid.cs  70  13  PCID Grabber

推荐答案

您还需要向项目添加程序集引用.您需要在项目中引用 System.Management 程序集,否则 using 语句将无法找到命名空间

You also need to add an assembly reference to your project. You need to reference the System.Management assembly in your project, otherwise the using statements will not be able to locate the namespaces

  1. 右键单击您的项目
  2. 点击添加参考..."
  3. 然后从程序集"标签中选择系统管理"

这篇关于C#错误-找不到命名空间ManagementClass,ManagementObject和ManagementObjectCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆