6

I would like to display the up time of the machine my code is running, how can I do that?

9 Answers 9

9

Try this link. It uses the System.Environment.TickCount property

Gets the number of milliseconds elapsed since the system started. - MSDN

http://msdn.microsoft.com/en-us/library/system.environment.tickcount(VS.80).aspx

Note: this method will work for 25 days because TickCount is an Int32.
4
  • 2
    Consequently, if the system runs continuously, TickCount will increment from zero to Int32.MaxValue for approximately 24.9 days, then jump to Int32.MinValue, which is a negative number, then increment back to zero during the next 24.9 days. How can I know if it runs more than 25 days???
    – Mister Dev
    Commented Nov 5, 2008 at 13:21
  • 12
    I guess Microsoft never expected a machine to stay up for more than 25 days. Commented Nov 5, 2008 at 14:21
  • Would a MS OS even run after not restarting that long? haha
    – Inisheer
    Commented Nov 7, 2008 at 5:44
  • 2
    I know a guy with WinXP who's had his laptop running for the better part of a year, so yeah, it would :P
    – Sukasa
    Commented Jun 9, 2009 at 20:46
5
using System.Management;
using System.Linq;

TimeSpan GetUptime()
 { var query = new SelectQuery("SELECT LastBootUpTime 
                                 FROM Win32_OperatingSystem 
                                 WHERE Primary='true'");
   var mos = new ManagementObjectSearcher(query);
   var str = mos.Get().First().Properties["LastBootUpTime"].Value.ToString();

   return DateTime.Now - ManagementDateTimeConverter.ToDateTime(str);
 }

(Based on code from http://bytes.com/forum/thread502885.html)

4

Curious that none of them considered Stopwatch yet?

Precise and bigger than System.Environment.TickCount, not involving OS horrific perf counters, WMI or native calls, and well really simple:

var ticks = Stopwatch.GetTimestamp();
var uptime = ((double)ticks) / Stopwatch.Frequency;
var uptimeSpan = TimeSpan.FromSeconds(uptime);
2

You can also use Diagnostics

using System.Diagnostics;
..........
PerformanceCounter perfc = new PerformanceCounter("System","System Up Time");
perfc.NextValue();
TimeSpan t = TimeSpan.FromSeconds(perfc.NextValue());
..........
1
  • 1
    Doesn't show me the real time. Should be 25h30min and it display 15hours18mins
    – Mister Dev
    Commented Nov 5, 2008 at 17:24
1

The simplest and proper way to do this is

public static TimeSpan GetUptime()
{
    ManagementObject mo = new ManagementObject(@"\\.\root\cimv2:Win32_OperatingSystem=@");
    DateTime lastBootUp = ManagementDateTimeConverter.ToDateTime(mo["LastBootUpTime"].ToString());
    return DateTime.Now.ToUniversalTime() - lastBootUp.ToUniversalTime();
}
1
  • This returns an exception with the message:Invalid object path on Windows 2003 Server running in a web service in IIS6 Commented Jan 11, 2011 at 15:59
0

For future reference, on *nix, use uptime(1)

2
0

Just as a reference; in the Win32 world, you can use:

GetTickCount, GetTickCount64, or Performance Counters

-1

I suggest you to use the command line : net statistics workstation and parse the output. The time that machine is running is after "Statistics since ".

5
  • 1
    If you want to retrieve this from .net this is a bad approach. You are relying on "net" from having the same format output in the future, and that it will be available and included on all machines. Commented Dec 30, 2008 at 17:32
  • 1
    Command line output can also vary from one OS version to another, which can make it fail after deployment. I had this happen from XP to Vista, and the only difference was an extra carriage return in one OS vs the other. Commented Dec 30, 2008 at 18:03
  • 1
    If the Workstation is restarted, this will (I believe) give an incorrect value
    – SLaks
    Commented Jun 9, 2009 at 20:15
  • 2
    I meant the Workstation service
    – SLaks
    Commented Jun 24, 2009 at 16:55
  • 1
    This is a terrible solution and should not be the accepted answer. 1) Command-line output may vary between OS. 2) It only tells you when the Workstation service was last started/restarted. 3) The workstation service is not guaranteed to even be running at all. 4) Doesn't work on unix, even with samba installed. Commented Jun 28, 2017 at 4:56
-1

There's many definitions of what up could mean. Assuming we are talking about an application, then a remote ping of a simple service might suffice. Keynote provide enterprise level solutions for the web and there must be many others out, many free i would imagine.

UPDATE: given this was tagged .net I assumed we are interested in the uptime of an application. Is it an application within which you want to show uptime of the machine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.