[ 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_OperatingSystemFigure 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_OperatingSystemFigure 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();
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.
public void BindOperatingSystemDataGrid()
OperatingSystemDataGrid.DataSource = GetOperatingSystemDetails();
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
Now execute this web page in your browser and you should have a DataGrid similar to the following.
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.