我一直在尝试将WMI用于我的代码,我不能使用System.Management类,因为它们不存在。我在3.5和4网上尝试过它。什么都行不通。 我没有找到任何问题的解决方案,并想知道你们有没有遇到过这个问题?如果是这样,为什么我只有:
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 Ultimate 使用net 3.5 - Net 4。
不确定我在这里缺少什么。
答案 0 :(得分:4)
System.Management.Instrumentation
不是类,它是命名空间。根据您使用的using指令,如果您有对System.Management.dll的引用,您应该能够编写如下代码:
ObjectQuery query = new ObjectQuery("...");
答案 1 :(得分:1)
您是否添加了对System.Management的引用?另外,请确保您定位的框架版本不是客户端配置文件。
答案 2 :(得分:1)
MSDN在System.Management.Instrumentation
命名空间下列出了很多类:
DefaultManagementInstaller Class DefaultManagementProjectInstaller Class IEvent Interface IgnoreMemberAttribute Class IInstance Interface Instance Class InstanceNotFoundException Class Instrumentation Class InstrumentationBaseException Class InstrumentationClassAttribute Class InstrumentationException Class InstrumentationManager Class InstrumentationType Enumeration InstrumentedAttribute Class ManagedCommonProvider Class ManagedNameAttribute Class ManagementBindAttribute Class ManagementCommitAttribute Class ManagementConfigurationAttribute Class ManagementConfigurationType Enumeration ManagementCreateAttribute Class ManagementEntityAttribute Class ManagementEnumeratorAttribute Class ManagementHostingModel Enumeration ManagementInstaller Class ManagementKeyAttribute Class ManagementMemberAttribute Class ManagementNameAttribute Class ManagementNewInstanceAttribute Class ManagementProbeAttribute Class ManagementQualifierAttribute Class ManagementQualifierFlavors Enumeration ManagementReferenceAttribute Class ManagementRemoveAttribute Class ManagementTaskAttribute Class WmiConfigurationAttribute Class WmiProviderInstallationException Class
这些住在System.Management.dll
- 请确保添加对它的引用。
答案 3 :(得分:1)
我相信你需要添加对System.Management。* .dll文件的引用。
如果你来自像我这样的C ++背景,我猜你有过与我过去相同的概念问题,当时我认为在c#中使用语句类似于C / C ++中的include语句。这是一个微妙的差异,但它导致我多年的轻微混乱,几年前我拿到反射器时终于清理了......
答案 4 :(得分:0)
右键单击项目的References文件夹,然后选择Add Reference ...并从.NET选项卡中选择System.Management.dll。 (您可能需要等待一段时间才能显示DLL,此列表会延迟加载。)
此外,请确保在Project Properties中没有定位.NET Framework Client Profile:您很可能需要完整的.NET Framework 4.0。
为什么没有这样做会看到任何东西?相当令人困惑的是,.NET库中的程序集和名称空间之间不一定存在一对一的关系。
因此,即使您没有包含对System.Management.dll 程序集的引用,您仍会在System.Management命名空间中看到一个类 - 这包含在其中一个您已经引用的其他程序集,或系统核心程序集本身。
您可以通过将自己的类添加到System.Management命名空间来自己尝试这个技巧:
namespace System.Management
{
public class MyClass
{
}
}
这将以项目属性中命名的程序集结束,但属于名称空间 System.Management
。
注意,打破命名空间名称和程序集名称之间的关系会非常混乱,所以不要!