tags:

views:

597

answers:

8

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

A: 

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 ".

Daok
Accepted this one since it's the only one that really works.
Mister Dev
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.
Scott Markwell
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.
JosephStyons
If the Workstation is restarted, this will (I believe) give an incorrect value
SLaks
I meant the Workstation service
SLaks
A: 

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.

dove
+6  A: 

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.
JTA
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
I guess Microsoft never expected a machine to stay up for more than 25 days.
Bill the Lizard
Would a MS OS even run after not restarting that long? haha
JTA
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
+2  A: 

You can also use Diagnostics

using System.Diagnostics;
..........
PerformanceCounter perfc = new PerformanceCounter("System","System Up Time");
perfc.NextValue();
TimeSpan t = TimeSpan.FromSeconds(perfc.NextValue());
..........
Bogdan
Doesn't show me the real time. Should be 25h30min and it display 15hours18mins
Mister Dev
A: 

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

gnud
There must be a C (3) call, right? What API does uptime(1) call to get this information?
Stéphane
Well, on Linux, GNU coreutils just reads /proc/uptime, Read the full sources at http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/uptime.c
gnud
+2  A: 
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)

Mark Cidade
A: 

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

GetTickCount, GetTickCount64, or Performance Counters

JosephStyons
A: 

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