Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / Windows Forms
Article

Wmi2Dataset

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
17 Nov 2007CPOL2 min read 25.9K   691   16   1
Make Queries over WMI as in the Query Analyzer
Screenshot - WMI2Dataset

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:

  1. Create a ManagementScope object
  2. Create a ObjectQuery object with the user's query
  3. Create a ManagementObjectSearcher object with the query and the scope
  4. Obtain the result and store it in a ManagementObjectCollection
  5. Create a dataTable; generate structure from the result
  6. Fill the dataTable
C#
public static DataTable QueryWmiADataTable(string query, string nombreTabla) {
    ConnectionOptions oConn = new ConnectionOptions();
    //oConn.Username = "DOMAIN\\USER";
    //oConn.Password = "";
    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));
                // Realizado por Jhonny D. Cano  jhonnycano at hotmail.com
            }
        }

        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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Colombia Colombia
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 .

Comments and Discussions

 
GeneralPowerShell Pin
Evyatar Ben-Shitrit18-Nov-07 5:10
Evyatar Ben-Shitrit18-Nov-07 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.