This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Executing WMI Queries

Windows Management Instrumentation provides a number of services that allow gathering of information about the operating system, software and hardware of a machine. Using WQL, this information can be retrieved using familiar, text-based queries.

Data Types

When retrieving information using WQL queries, the values accessed via the ManagementObject indexer are always returned as objects. In most cases these objects can be cast to strings, numeric types or Boolean values using the cast operator. One key exception to this is date and time data. This information cannot directly be cast to a DateTime type. To understand why, try executing the following WQL query, which returns the current local time. Change the code in the loop to output the LocalDateTime value.

SELECT LocalDateTime FROM Win32_OperatingSystem

When you view the date and time information, you will see that it is held in a string and looks similar to the following:


The structure of the string is easy to see. The first four digits are the year and are followed by two digits each for the month, date, hour, minute and second. Six digits are provided to show a fraction of a second. The final four characters show the offset of the local time, in minutes, compared to Universal Co-ordinated Time (UTC). If we want to convert this to a DateTime that can be used in C#, we can use the DateTime.ParseExact method, omitting the offset because this is a local time. This is shown below:

string dateString = (string)result["LocalDateTime"];
string trimmed = dateString.Substring(0, 21);
string format = "yyyyMMddHHmmss.ffffff";
DateTime date = DateTime.ParseExact(trimmed, format, CultureInfo.InvariantCulture);
9 October 2010