Introduction
Hello there. This is my second article on CodeProject. I hope you find it useful. This is a utility designed for obtaining information from a computer through the WMI (Windows Management Instrumentation) technology.
Background
There are a lot of references for using WMI in MSDN, however, I was wondering if there was a way of querying the information system in a way that was convenient for me. While looking at the syntax of obtaining the information (almost the same as SQL), I wished there was a Query Analyzer or something like that.... I searched but did not find one, so I began this little project and voila!
Using the Code
The core of the code in a nutshell is as follows:
- Create a
ManagementScope
object - Create a
ObjectQuery
object with the user's query - Create a
ManagementObjectSearcher
object with the query and the scope - Obtain the result and store it in a
ManagementObjectCollection
- Create a
dataTable
; generate structure from the result - Fill the
dataTable
public static DataTable QueryWmiADataTable(string query, string nombreTabla) {
ConnectionOptions oConn = new ConnectionOptions();
ManagementScope msAlcance = new ManagementScope("\\\\localhost", oConn);
ObjectQuery oqQuery = new ObjectQuery(query);
ManagementObjectSearcher mosBuscador =
new ManagementObjectSearcher(msAlcance, oqQuery);
ManagementObjectCollection moColColeccion = mosBuscador.Get();
DataTable dt = new DataTable(nombreTabla);
foreach (ManagementObject obj in moColColeccion) {
if (dt.Columns.Count == 0) {
IEnumerator ie = obj.Properties.GetEnumerator();
while (ie.MoveNext()) {
PropertyData p = ie.Current as PropertyData;
if (p != null) dt.Columns.Add(p.Name, TipoCIMATipo(p.Type));
}
}
DataRow dr = dt.NewRow();
foreach (DataColumn dc in dt.Columns) {
object valor;
if (dc.DataType.Equals(typeof(DateTime))) {
valor = ToDateTime(obj[dc.ColumnName]);
} else {
valor = (obj[dc.ColumnName]);
}
dr[dc] = valor ?? DBNull.Value;
}
dt.Rows.Add(dr);
}
return dt;
}
The program submits the query with the F5 key, creating a DataTable with the name in the TextBox right up from the results grid. Note that the WMI works with different data types than C#, so in some cases (DateTime
) it is necessary to convert the result for storing in the DataTable
. In the code, there is the conversion function obtained from somewhere on the Internet. I also provided some sample queries at the right of the window. With double click, they will pass to the query text area.
You can save the query in a Text file, and the results in an XML format, it could be adapted for example for centralizing the hardware information of a network.
Another utility added to the application is the object visualizer for viewing the field returned when this is an array (For example in Win32_NetworkAdapterConfiguration
).
Points of Interest
It has been very useful for me to develop this tool because it helped me understand the WMI classes much better. However, this is an extensive field, so I would ask you to research more on this topic.
History
- 15-11-2007: First Release
I am Jhonny D Cano. I'm from Colombia, i'm professional in systems engineering. I worked since 1998 in Visual Basic and C++, then in 2.003 I jumped out to .Net development platform (VB.Net) and then i started with C#. I have a solid background in Windows Forms and in my current job I am working with ASP.Net and Web Services over Oracle and SQL Server. Alternatively I got the SUN Certification "Java Certified Programmer" and currently i'm in the process of 70-356's certification .