Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve system uptime using C#

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

like image 949
ProgrammingPope Avatar asked Jun 09 '09 19:06

ProgrammingPope


People also ask

How do you read uptime proc?

Using /proc/uptimeThe first number is the total number of seconds the system has been up. The second number is how much of that time the machine has spent idle, in seconds. On multi core systems (and some Linux versions) the second number is the sum of the idle time accumulated by each CPU.

How do I view uptime?

Right-click the Windows taskbar and select Task Manager. Once Task Manager is open, click the Performance tab. Under the Performance tab, you will find the label Uptime.

What is the command for uptime?

To check your computer uptime using Command Prompt, use these steps: Open Start. Search for Command Prompt, right-click the top result, and click the Run as administrator option. Type the following command to query the device's last boot time and press Enter: wmic path Win32_OperatingSystem get LastBootUpTime.

How do you show how long the system has been running?

For Windows Home Server, XP, or Server 2003, go to Start >> Run and type: cmd, then hit Enter. At the command prompt, type: net stats srv then hit Enter. You'll see the line Statistics Since – this shows the date and time that your system has been running.


1 Answers

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());         }     } } 
like image 158
SLaks Avatar answered Sep 18 '22 17:09

SLaks