C# – Retrieve system uptime using C#

cnetuptimewindows

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

Best Answer

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());
        }
    }
}