2011-10-01 81 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

嗯。我會做得更清楚。現在好了嗎? –