Category Archives: Visual C#

Using WMI Code Creator for projects

WMI Code Creator v1.0 – Download Here
Allows you to browse the available WMI classes and create query’s that can be used either through command-line through visual basic script or in your .NET projects.

Once you have your query you can then export the code for Visual C# or Visual Basic.

I used this program to create some code for a quick way of viewing installed printers on a remote print server in Visual C# by populating the results to a datagrid.

                ConnectionOptions connection = new ConnectionOptions();
                connection.Username = userName.Text;
                connection.Password = password.Text;
                connection.Authority = "ntlmdomain:DOMAINNAME";
                ManagementScope scope = new ManagementScope(
                    "\\\\" + serverName.Text + ".FullyQualifiedDomainName\\root\\CIMV2", connection);

                ObjectQuery query = new ObjectQuery(
                    "SELECT * FROM Win32_Printer"); 

                ManagementObjectSearcher searcher =  new ManagementObjectSearcher(scope, query);

                    DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
                    row.Cells[0].Value = queryObj["DeviceID"];
                    row.Cells[1].Value = queryObj["DriverName"];
                    row.Cells[2].Value = queryObj["Location"];
                    row.Cells[3].Value = queryObj["Name"];
                    row.Cells[4].Value = queryObj["Comment"];
                    row.Cells[5].Value = queryObj["Portname"];
                    row.Cells[6].Value = queryObj["SystemName"];
            catch (ManagementException e)
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);

Fast and effective to what I needed – Instead of trying to copy all the printer details out in to excel.