Retrieve system uptime using C#

ProgrammingPope picture ProgrammingPope · Jun 9, 2009 · Viewed 44.5k times · Source

Is there a simple way to get a system's uptime using C#?

Answer

SLaks picture SLaks · Jun 9, 2009
public TimeSpan UpTime {
    get {
        using (var uptime = new PerformanceCounter("System", "System Up Time")) {
            uptime.NextValue();       //Call this an extra time before reading its value
            return TimeSpan.FromSeconds(uptime.NextValue());
        }
    }
}