Introducing Windows Management Instrumentation (WMI)
page 3 of 4
by Steven Swafford
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27714/ 48

Build your first WQL Query

[ download code ]

Let us say that we want to gather build number, build type, and version of the operating system. In order to do this we first need to establish the WQL query. In this case we will utilize the following query:

OS WQL Query

SELECT * FROM Win32_OperatingSystem
Figure 4

As you are probably all too familiar with you really should not SELECT * in any query whether SQL or WQL unless you are going to utilize all the return results. So our OS WQL query really should look like the following:

SELECT BuildNumber, BuildType, Version FROM Win32_OperatingSystem
Figure 4-1

Example GetOperatingSystemDetails Method

public ArrayList GetOperatingSystemDetails()
{
 ArrayList osCollection = new ArrayList();
 ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT BuildNumber, BuildType, Version FROM Win32_OperatingSystem");
 
 foreach(ManagementObject osObject in mos.Get())
 {
  IOperatingSystem os = new IOperatingSystem();
  os.BuildNumber = osObject["BuildNumber"].ToString();
  os.BuildType = osObject["BuildType"].ToString();
  os.Version = osObject["Version"].ToString();
  osCollection.Add(os);
 }
 return osCollection;
}
Figure 5

The method GetOperatingSystemDetails performs four separate actions.

  • Establishes a new ArrayList named osCollection.
  • Establishes a new ManagementObjectSearcher named mos.
  • Loops thru the osObject and adds each object to osCollection.
  • Finally the osCollection is returned.

Now that you have been provided the foundation of writing a WQL query to gather operating system details let us now apply this instruction into a working demo using a DataGrid. We will utilize both the example query and method above (see figure 4 and 5).

Within your IDE create a DataGrid and name it OperatingSystemDataGrid.

<asp:DataGrid id="OperatingSystemDataGrid" runat="server"></asp:DataGrid>
Figure 6

If you recall the GetOperatingSystemDetails method all you need to do at this point is establish a new method which will in turn set the OperatingSystemDatagrid’s DataSource as well as DataBind the DataGrid.

Example

public void BindOperatingSystemDataGrid()
{
 OperatingSystemDataGrid.DataSource = GetOperatingSystemDetails();
 OperatingSystemDataGrid.DataBind();
}
Figure 7

Now in you page load method simply call the BindOperatingSystemDataGrid() method.

private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
 BindOperatingSystemDataGrid();
}
Figure 8

Now execute this web page in your browser and you should have a DataGrid similar to the following.

Figure 9
Figure 9

As you can see it really is not all that difficult to use WQL and construct the code that will in turn display the desire information to the end user. Now that you have the foundation of WQL there is another avenue we can take to achieve the same results of which I will discuss in greater details.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 4:51:38 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search