|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis is the third article in the series of articles How To: (Almost) Everything In
In this article, I'm going to introduce a simple framework that can be used to aggregate property values for any of the hundreds of classes in the In the previous additions, we focused on a way to provide the property values as well as executing Included ClassesThe following included classes will return a collection of all the properties and values. This only applies to single
Using the Attached CodeMethods (Local Machine or Remote Machine)
Instantiate the Local Provider//Local Connection
Connection wmiConnection = new Connection();
Instantiate the Remote Provider//Remote Connection
//Requires UserName, Password, Domain, and Machine Name
Connection wmiConnection = new Connection("neal.bailey",
"3l!t3p@$$",
"BAILEYSOFT",
"192.168.2.100");
Using the ClassesUsing the classes is very simple. You just create a new instance of the Connection wmiConnection = new Connection(); //The local or remote connection object
Win32_Battery b = new Win32_Battery(wmiConnection); //Create the WMI Class you want
foreach (string property in b.GetPropertyValues()) //enumerate the collection
{
Console.WriteLine(property);
}
Extending To Add Your Own WMI ClassesThis solution is a simple framework that anyone can use to add and remove the WMI classes they need to enumerate. Step One: Find some classes you want from the Microsoft WMI SDK. Step Two: Create a class with the same name as the using System;
using System.Collections.Generic;
using System.Text;
namespace baileysoft.Wmi
{
class Win32_Printer : IWMI //The class name is the ONLY part that changes
{
Connection WMIConnection;
//The class name is the ONLY part that changes
public Win32_Printer(Connection WMIConnection)
{
this.WMIConnection = WMIConnection;
}
public IList{string} GetPropertyValues() //replace curly braces with <>
{
string className = System.Text.RegularExpressions.Regex.Match(
this.GetType().ToString(), "Win32_.*").Value;
return WMIReader.GetPropertyValues(WMIConnection,
"SELECT * FROM " + className,
className);
}
}
}
Step Three: Create a new tag in the settings.xml file for your class and enter the properties you want to enumerate (those ones in the SDK). Refer to the settings.xml file for the existing examples. ConclusionThe History
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||