Tuesday, August 3, 2010

Read Time Since Startup

The following snippet shows how to determine the time, the computer is already running since starting, in C#.
With System.Environment.TickCount this value in milliseconds can be read-out in milliseconds.
The example converts this then to an object of the type TimeSpan. This class managages time spans, this way the count of milliseconds can be converted easily in hours, minutes and seconds:

int MsSinceStartup = System.Environment.TickCount; // timespan since startup in milliseconds
TimeSpan TimeDifference = new TimeSpan(0,0,0,0, MsSinceStartup);
MessageBox.Show("Zeit seit Hochfahren: " + TimeDifference.Hours + "h " + TimeDifference.Minutes + "min " + TimeDifference.Seconds + "s"); // output

No comments:

Post a Comment