boot.initimeout:Determine the Time since the Last Boot Up in C#

Introduction:

This article describes an approach used to determine the time of the last system boot up and to display the time elapsed since boot up. The application uses element of the System.Management library to run a select query against Win32_OperatingSystem; the application also display the amount of elapsed time since system boot up by comparing the boot up time against the current time; the elapsed time is updated every 1000 milliseconds whilst the application is running. A TimeSpan object is used to calculate the difference between the current time and the boot up time.

Figure 1: The Application

Getting Started:

In order to get started, unzip the included project and open the solution in the Visual Studio 2008 environment. In the solution explorer, you should note these files (Figure 2):

Figure 2: Solution Explorer

As you can see from Figure 2; there is a single Win Forms project containing a single form. All code required of this application is included in this form's code.

The Main Form (Form1.cs).

The main form of the application, Form1, contains all of the code necessary.

If you'd care to open the code view up in the IDE you will see that the code file begins with the following library imports:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Management;

Note that the defaults have been altered and now include the reference to the System.Management library.

Following the imports, the namespace, class, and constructor are defined:

namespace TimeSinceBootup

{

public partial class Form1 : Form

{

Next up, a local variable is defined:

// local member variable

private DateTime dtBootTime = new DateTime();

This variable is used to store the last boot up time; this value is set when the application starts and is used in each subsequent evaluation the elapsed time (in response to a timer tick event).

The next section of code is the form's constructor; the _disibledevent="Verdana, Arial, Helvetica, sans-serif">This article is intended to describe an approach to obtaining the time since last system boot up through the use of the System.Management library. The same approach may be used to extract other bits of information from the system in a similar fashion.

Tags:  determine的名词 determine的用法 determine boot.initimeout

延伸阅读

最新评论

发表评论