views:

279

answers:

4

Does anyone know of a way to programatically find the uptime of a server running Windows 2000? We have a service running on the machine written in VB.NET, that reports back to our server via a webservice.

A: 

You can use WMI: see my answer to this question.

fretje
+1  A: 

Another way is to use the performance counters from .NET (sorry, this is in C# but you can easily convert over to VB) e.g.

 var pc = new PerformanceCounter("System", "System Up Time");

 pc.NextValue(); // This returns zero for a reason I don't know

 // This call to NextValue gets the correct value
 TimeSpan ts = TimeSpan.FromSeconds(pc.NextValue());

So basically, the PerformanceCounter class will return the number of seconds the system has been up and from there you can do what you want.

KnackeredCoder
Thanks, this worked for me. As a reference for other people here is the converted VB code I used:Dim uptimeTs As New TimeSpan()Dim pc As New PerformanceCounter("System", "System Up Time") pc.NextValue() uptimeTs = TimeSpan.FromSeconds(pc.NextValue())
Brian
Please excuse the formatting, I'm new to this site and am still getting used to getting newlines in comments
Brian
A: 

Other approach: Use a (free) website monitoring service such as AlertFox?

A: 

If you have SNMP enabled, you can query the following OID: 1.3.6.1.2.1.1.3.0. This will give you the system uptime. It is defined as "The time (in hundredths of a second) since the network management portion of the system was last re-initialized."