Sometime ago I use to compare my OS uptime with some friends; it was like an Uptime Competition (We had a lot of free time ;-)). I don't know why but today I remembered it and I thought about if I could obtain my OS Uptime using C#. About one year ago or so I worked a bit with WMI when I was coding against the Windows Media Server API, and since then I think that WMI is a good way to obtain information about our system.
I've checked the WMI reference at MSND and I got a WMI class that has the last time boot up date. The class that hosts this date is Win32_OperatingSystem among other data; such Windows install date and other interesting things. Once we have that the only thing we can do is... to Code !!
The method shown below obtains the last boot up time from the property LastBootUpTime of the Win32_OperatingSystem class, so I created ManagementClass object to get the instances of the WMI class. Then I looped thought it elements to obtain the date, it's easy.
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
1
///
2
///
Obtains the OS uptime
3
///
4
///
TimeSpan object that contains the uptime
5
public
TimeSpan GetUptime()
6
{
7
//
timespan object to store the result value
8
TimeSpan uptimeTs
=
new
TimeSpan();
9
10
//
management objects to interact with WMI
11
ManagementClass management
=
new
ManagementClass(
"
Win32_OperatingSystem
"
);
12
ManagementObjectCollection mngInstance
=
management.GetInstances();
13
14
//
loop throught the mngInstance
15
foreach
(ManagementObject mngObject
in
mngInstance)
16
{
17
//
get the LastBootUpTime date parsed
18
DateTime lastBootUp
=
ParseCIMDateTime(mngObject[
"
LastBootUpTime
"
].ToString());
19
20
//
check it value is not DateTime.MinValue
21
if
(lastBootUp
!=
DateTime.MinValue)
22
{
23
//
get the diff between dates
24
uptimeTs
=
DateTime.Now
-
lastBootUp;
25
}
26
}
27
28
//
return the uptime TimeSpan
29
return
uptimeTs;
30
}
The DateTime format that WMI works with is the CIM_DATETIME. The dates are represented as strings with a specific format. This format is covered in depth at WMI reference at MSDN. But the date format isn't a problem, it's a human readable format. The string 20070112204559.115081+060 represents 12/01/2007 20:45:59,115, so I created a method to parse CIM dates to .Net DateTime objects.
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
1
///
2
///
Parses a CIM_DateTime string
3
///
4
///
5
///
The CIM_DateTime format is represented by the following string:
6
///
yyyy MM dd hh mm ss.mmm mmm UTC
7
///
2007 01 12 20 45 59.115 081+060
8
///
9
///
Input CIM_DateTime string
10
///
DatTime object that represents the CIM_DateTIme input string
11
private
static
DateTime ParseCIMDateTime(
string
wmiDate)
12
{
13
//
datetime object to store the return value
14
DateTime date
=
DateTime.MinValue;
15
16
//
check date integrity
17
if
(wmiDate
!=
null
&&
wmiDate.IndexOf(
'
.
'
)
!=
-
1
)
18
{
19
//
obtain the date with miliseconds
20
string
tempDate
=
wmiDate.Substring(
0
, wmiDate.IndexOf(
'
.
'
)
+
4
);
21
22
//
check the lenght
23
if
(tempDate.Length
==
18
)
24
{
25
//
extract each date component
26
int
year
=
Convert.ToInt32(tempDate.Substring(
0
,
4
));
27
int
month
=
Convert.ToInt32(tempDate.Substring(
4
,
2
));
28
int
day
=
Convert.ToInt32(tempDate.Substring(
6
,
2
));
29
int
hour
=
Convert.ToInt32(tempDate.Substring(
8
,
2
));
30
int
minute
=
Convert.ToInt32(tempDate.Substring(
10
,
2
));
31
int
second
=
Convert.ToInt32(tempDate.Substring(
12
,
2
));
32
int
milisecond
=
Convert.ToInt32(tempDate.Substring(
15
,
3
));
33
34
//
compose the new datetime object
35
date
=
new
DateTime(year, month, day, hour, minute, second, milisecond);
36
}
37
}
38
39
//
return datetime
40
return
date;
41
}
This is all the logic that we need to obtain our OS uptime. I wrote a small console application that outputs the OS uptime in the format:
"Uptime: 7 days, 2 hours, 2 minutes, 7 seconds and 317 milliseconds"
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->
1
///
<summary>
2
///
Console entry point
3
///
</summary>
4
private
static
void
Main()
5
{
6
//
create an instace of the UpTime class
7
Uptime uptime
=
new
Uptime();
8
9
//
obtain the TimeSpan that represents the uptime
10
TimeSpan ts
=
uptime.GetUptime();
11
12
//
write the uptime to the output
13
Console.WriteLine(String.Format(
"
Uptime: {0} days, {1} hours, {2} minutes, {3} seconds and {4} miliseconds
"
,
14
ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds));
15
}

WMI Reference at MSDN: http://msdn2.microsoft.com/en-us/library/aa394582....
Win32_OperatingSystem Class reference: http://msdn2.microsoft.com/en-us/library/aa394239....
CIM_DateTime format reference: http://msdn2.microsoft.com/en-us/library/aa387237....
That's all folks!! I hope you like it