Skip to content

Instantly share code, notes, and snippets.

@WildGenie
Created February 15, 2016 12:10
Show Gist options
  • Save WildGenie/327479a42b7ecc79282d to your computer and use it in GitHub Desktop.
Save WildGenie/327479a42b7ecc79282d to your computer and use it in GitHub Desktop.
TimeSpan ts = TimeSpan.FromMilliseconds(Environment.TickCount);
Console.WriteLine("{0}d {1}h {2}m {3}s", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
//---------------------------
using System.Diagnostics;
PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
upTime.NextValue();
TimeSpan ts = TimeSpan.FromSeconds(upTime.NextValue());
Console.WriteLine("{0}d {1}h {2}m {3}s", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
//-------------------------
public TimeSpan UpTime {
get
{
using (var uptime = new PerformanceCounter("System", "System Up Time"))
{
uptime.NextValue(); // Must call extra time before reading its value
return TimeSpan.FromSeconds(uptime.NextValue());
}
}
}
//---------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment