2011-10-01 37 views
1

我一直在尝试为我的代码使用WMI,我不能使用System.Management类,因为它们不在那里。我已经在3.5和4网上尝试过了。什么都没有 我还没有找到任何解决方案的问题,并想知道你是否曾经遇到过这个问题?如果是这样,那为什么我只有:C#System.Management只有一个类?

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旗舰版 使用net 3.5 - 净4.

不知道我在这里失踪。

+0

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

回答

1

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

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 - 确保你添加一个引用到它。

+0

事情是,我试过引用它,但没有快乐...... – Meh

1

我相信你需要添加对System.Management。*。dll文件的引用。

如果你来自像我这样的C++背景,我猜你在过去使用c#中的语句来查看类似于C/C++中的语句时,有过类似的概念问题。这是一个微妙的差异,但它使我多年的非常轻微的困惑终于放晴了,当我几年前买了反射镜的保持......

4

System.Management.Instrumentation不是类,这是一个命名空间。由于使用的指令,你已经得到了,如果你要System.Management.dll一个参考,你应该能够编写代码,如:

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

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

另外,请确保在Project Properties中未定位.NET Framework Client Profile:很可能需要完整的.NET Framework 4.0。

为什么你看到什么都没有做到这一点?相当混淆的是,.NET库中的程序集名称空间之间不一定是一对一的关系。

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

你可以通过将自己的类添加到系统来尝试这个技巧。管理命名空间:

namespace System.Management 
{ 
    public class MyClass 
    { 
    } 
} 

这将在装配在你的项目属性命名结束,但将属于命名空间System.Management

请注意,打破名称空间名称和程序集名称之间的关系会非常混乱,所以不要这么做!

+0

这个答案很好地解释了为什么问题存在,但不是如何解决它。 –

+0

嗯。我会做得更清楚。现在好了吗? –