I have been trying to utilize WMI for my code, I cannot use the System.Management classes as they are not there开发者_如何学Go. I have tried it on 3.5 and 4 Net. Nothing works. I have not found any solutions to the issue and was wondering if any of you have ever encountered this? If so, why is it that i only have:
System.Management.Instrumentation
My using
block below:
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;
Info:
Visual Studio 2010 Ultimate Using net 3.5 - Net 4.
Not sure what I am missing here.
System.Management.Instrumentation
isn't a class, it's a namespace. Given the using directives you've got, if you've got a reference to System.Management.dll, you should be able to write code such as:
ObjectQuery query = new ObjectQuery("...");
MSDN lists quite a lot of classes under the System.Management.Instrumentation
namespace:
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
These live in System.Management.dll
- make sure you add a reference to it.
I believe you need to add references to System.Management.*.dll files.
If you come from a C++ background like me I'm guessing you have the same conceptual issue I had in the past when I took the view of using statements in c# as analogous to include statements in C/C++. It's a subtle difference but it led me to years of very slight confusion that finally cleared up when I got a hold of reflector a few years ago...
Did you add a reference to System.Management? Also make sure the framework version you're targeting isn't the Client Profile.
Right-click on the References folder for the project, then choose Add Reference... and from the .NET tab choose System.Management.dll. (You may have to wait a short while for the DLL to appear, this list is lazily-loaded.)
Also, make sure in Project Properties that you're not targetting the .NET Framework Client Profile: you'll most likely need the full .NET Framework 4.0.
Why do you see anything at all without doing this? Rather confusingly there isn't necessarily a one-to-one relationship between assemblies and namespaces in the .NET libraries.
So, even if you don't include a reference to the System.Management.dll assembly, you will still see one class in the System.Management namespace -- this is included in one of the other assemblies you've referenced already, or the system core assembly itself.
You can try this trick yourself by adding your own class to the System.Management namespace:
namespace System.Management
{
public class MyClass
{
}
}
This will end up in the assembly named in your project properties, but will belong to the namespace System.Management
.
Note, it gets terribly confusing to break the relationship between namespace names and assembly names, so don't!
精彩评论