【问题标题】:C# System.Management has only 1 class?C# System.Management 只有 1 个类?
【发布时间】:2011-10-01 18:32:23
【问题描述】:

我一直在尝试将 WMI 用于我的代码,但我无法使用 System.Management 类,因为它们不存在。我已经在 3.5 和 4 Net 上试过了。没有任何效果。 我还没有找到任何解决这个问题的方法,想知道你们中是否有人遇到过这个问题?如果是这样,为什么我只有:

System.Management.Instrumentation

下面是我的using 块:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Collections;
using System.Management.Instrumentation;

信息:

Visual Studio 2010 终极版 使用网络 3.5 - 网络 4。

不确定我在这里缺少什么。

【问题讨论】:

  • 除了 Jon Skeet 所说的,请确保您没有使用客户端配置文件。

标签: c# class wmi system.management


【解决方案1】:

System.Management.Instrumentation 不是一个,它是一个命名空间。鉴于您拥有的 using 指令,如果您有对 System.Management.dll 的引用,您应该能够编写如下代码:

ObjectQuery query = new ObjectQuery("...");

【讨论】:

    【解决方案2】:

    MSDN 在System.Management.Instrumentation 命名空间下列出了相当多的类:

    DefaultManagementInstaller 类 DefaultManagementProjectInstaller 类 事件接口 IgnoreMemberAttribute 类 实例接口 实例类 InstanceNotFoundException 类 仪表类 InstrumentationBaseException 类 InstrumentationClassAttribute 类 InstrumentationException 类 InstrumentationManager 类 InstrumentationType 枚举 InstrumentedAttribute 类 ManagedCommonProvider 类 ManagedNameAttribute 类 ManagementBindAttribute 类 ManagementCommitAttribute 类 ManagementConfigurationAttribute 类 管理配置类型枚举 ManagementCreateAttribute 类 ManagementEntityAttribute 类 ManagementEnumeratorAttribute 类 ManagementHostingModel 枚举 ManagementInstaller 类 ManagementKeyAttribute 类 ManagementMemberAttribute 类 管理名称属性类 ManagementNewInstanceAttribute 类 ManagementProbeAttribute 类 管理限定符属性类 ManagementQualifierFlavors 枚举 管理参考属性类 ManagementRemoveAttribute 类 管理任务属性类 WmiConfigurationAttribute 类 WmiProviderInstallationException 类

    这些位于System.Management.dll - 确保添加对它的引用。

    【讨论】:

      【解决方案3】:

      我认为您需要添加对 System.Management.*.dll 文件的引用。

      如果你像我一样来自 C++ 背景,我猜你有同样的概念问题,我过去认为在 C# 中使用语句类似于在 C/C++ 中包含语句。这是一个微妙的差异,但它让我多年来一直感到非常轻微的困惑,几年前当我拿到反光板时终于明白了......

      【讨论】:

        【解决方案4】:

        您是否添加了对 System.Management 的引用?还要确保您定位的框架版本不是客户端配置文件。

        【讨论】:

          【解决方案5】:

          右键单击项目的 References 文件夹,然后选择 Add Reference... 并从 .NET 选项卡中选择 System.Management.dll。 (您可能需要稍等片刻才能显示 DLL,此列表是延迟加载的。)

          另外,请确保在项目属性中您没有针对 .NET Framework 客户端配置文件:您很可能需要完整的 .NET Framework 4.0。

          为什么不这样做你什么都看不到?相当令人困惑的是,.NET 库中的 程序集命名空间 之间不一定存在一对一的关系。

          因此,即使您不包含对 System.Management.dll 程序集 的引用,您仍然会在 System.Management 命名空间中看到一个类——它包含在其中之一中您已经引用的其他程序集,或系统核心程序集本身。

          您可以通过将自己的类添加到 System.Management 命名空间来自己尝试这个技巧:

          namespace System.Management
          {
             public class MyClass
             {
             }
          }
          

          这将在您的项目属性中命名的 程序集 中结束,但属于 命名空间 System.Management

          注意,打破命名空间名称和程序集名称之间的关系会变得非常混乱,所以不要!

          【讨论】:

          • 这个答案很好地解释了问题存在的原因,但没有解释如何解决它。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-13
          • 1970-01-01
          相关资源
          最近更新 更多