.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Windows Application >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Time and Date Details of Your System Using WMI Class

Posted By:Sanjeeb Lenka       Posted Date: November 29, 2013    Points: 200    Category: Windows Application    URL: http://www.dotnetspark.com  

This article describes how to get the Time and Date details of your system using a WMI Class. Here I will get the information from the Win32_CurrentTime class.
 

Introduction

This article describes how to get the Time and Date details of your system using a WMI Class. Here I will get the information from the Win32_CurrentTime class.

What Win32_CurrentTime is

The Win32_CurrentTime abstract is a singleton WMI class that describes a point in time by using the component items, such as milliseconds, seconds, minutes, hours, days, days of the week, week in the month, months, quarters, and years.

The following two important classes are derived from this class. Win32_LocalTime allows you to monitor time in local reference and Win32_UTCTime allows you to monitor time in coordinated universal time (UTC) reference. 

Design
 
Create a new Windows Forms Application Project.

Add one button control to the form.

Design your screen as in the following screen:

Next add a reference for "System.Management".

To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.


A window will open; in that choose the ".Net" tab.



It will show a list. In that list, choose "System.Management" and click the "OK" Button.



Now go to code view.

Add the namespace "using System.Management;".

Write the following code in the Button Click event
:

 

 private void button1_Click(object sender, EventArgs e)
        {
            SelectQuery Sq = new SelectQuery("Win32_CurrentTime");
            ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
            ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
            StringBuilder sb = new StringBuilder();
            foreach (ManagementObject mo in osDetailsCollection)
            {
                sb.AppendLine(string.Format("----------------------------"));
                sb.AppendLine(string.Format("Class : {0}", mo["__class"].ToString()));
                sb.AppendLine(string.Format("Day : {0}", mo["Day"].ToString()));
                sb.AppendLine(string.Format("DayOfWeek: {0}", mo["DayOfWeek"].ToString()));
                sb.AppendLine(string.Format("Hour: {0}", mo["Hour"].ToString()));
                sb.AppendLine(string.Format("Milliseconds: {0}", mo["Milliseconds"]).ToString());
                sb.AppendLine(string.Format("Minute: {0}", mo["Minute"].ToString()));
                sb.AppendLine(string.Format("Month : {0}", mo["Month"].ToString()));
                sb.AppendLine(string.Format("Quarter: {0}", mo["Quarter"].ToString()));
                sb.AppendLine(string.Format("Second : {0}", mo["Second"].ToString()));
                sb.AppendLine(string.Format("WeekInMonth: {0}", mo["WeekInMonth"].ToString()));
                sb.AppendLine(string.Format("Year : {0}", mo["Year"].ToString()));
            }
            MessageBox.Show(sb.ToString());
        } 
      

In the code above I am getting the information from Win32_CurrentTime and showing it in a Message Box on a button click.

SelectQuery

It represents a WMI Query Language (WQL) SELECT data query.

ManagementObjectSearcher

It retrieves a collection of management objects based on a specified query. 

This class is one of the more commonly used entry points to retrieve management information.

ManagementObjectCollection

It represents various collections of management objects retrieved using WMI.

Now build your application. Click on the button. It will show the
Current time and date (UTC Time and Local Time) details in a Message box.



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend