get system uptime in csharp

To get the system uptime in C#, you can subtract the current time from the system startup time. Here's an example code that does this using the TimeSpan class:

main.cs
// Get the system uptime
TimeSpan uptime = TimeSpan.FromMilliseconds(Environment.TickCount);

// Print the uptime in a human-readable format
Console.WriteLine($"System Uptime: {uptime.Days} days {uptime.Hours} hours {uptime.Minutes} minutes {uptime.Seconds} seconds");
269 chars
6 lines

The Environment.TickCount property returns the tick count of the system since it started. The TimeSpan.FromMilliseconds method converts this value to a TimeSpan object. Finally, you can format the TimeSpan object as a string to display it in a human-readable format.

gistlibby LogSnag