Windows uptime

  • Thread starter Thread starter Dylan Parry
  • Start date Start date
D

Dylan Parry

Hi,

I'm looking for a way to calculate the amount of time that Windows has
been running since the last boot. Ideally I'd like to be able to create
a DateTime object containing this value so that I can format it however
I like.

I've so far found a program called "uptime.exe" which is supplied by
Microsoft on their website, but it outputs the uptime value as a string
in the format "\\FRED has been up for: 5 day(s), 1 hour(s), 30
minute(s), 3 second(s)", which isn't of much use to me as it stands.

Is there a way of doing this in C#, or perhaps some method of running
"uptime.exe" and parsing the output to create a DateTime object?

Cheers,
 
Hello, Dylan!

DP> Is there a way of doing this in C#, or perhaps some method of running
DP> "uptime.exe" and parsing the output to create a DateTime object?

Win32 function GetTickCount() returns the time in millis elapsed since system start;

GetTickCount() can be accessed via P/Invoke
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Vadym Stetsyak said:
Hello, Dylan!

DP> Is there a way of doing this in C#, or perhaps some method of running
DP> "uptime.exe" and parsing the output to create a DateTime object?

Win32 function GetTickCount() returns the time in millis elapsed since system start;

GetTickCount() can be accessed via P/Invoke

Unfortunately, it wraps around every 49.7 days. It's a real pain in the
neck to get the right value out of Windows.

Either you've got to fiddle with HKEY_PERFORMANCE_DATA in the registry,
fiddle with performance counters (which are quite tricky in native code
like Delphi or C/C++), or you've got to call undocumented functions in
ntdll.dll (NtQuerySystemInformation with SystemProcessorTimes
(undocumented) is how Cygwin's uptime (via procps) does it).

Here's some C# code from
http://forums.webhostautomation.com...start=15&sid=649aff0d13a3cc760442eeac2963d4fa
which does the trick. It's amazingly slow for a one-off call, though:

---8<---
using System;
using System.Diagnostics;

class App
{
static void Main()
{
PerformanceCounter pc = new PerformanceCounter("System",
"System Up Time");
pc.NextValue();
TimeSpan ts = TimeSpan.FromSeconds(pc.NextValue());
Console.WriteLine(ts);
}
}
--->8---

It seems that its the initialization of the performance counter
libraries that causes the slowness, not the actual call to NextValue()
itself.

-- Barry
 
| Hi,
|
| I'm looking for a way to calculate the amount of time that Windows has
| been running since the last boot. Ideally I'd like to be able to create
| a DateTime object containing this value so that I can format it however
| I like.
|
| I've so far found a program called "uptime.exe" which is supplied by
| Microsoft on their website, but it outputs the uptime value as a string
| in the format "\\FRED has been up for: 5 day(s), 1 hour(s), 30
| minute(s), 3 second(s)", which isn't of much use to me as it stands.
|
| Is there a way of doing this in C#, or perhaps some method of running
| "uptime.exe" and parsing the output to create a DateTime object?
|
| Cheers,
|
| --
| Dylan Parry
| http://webpageworkshop.co.uk -- FREE Web tutorials and references

Only sure way is by using System.Management, following snip illustares
how...

using System.Management;
....

SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM
Win32_OperatingSystem WHERE Primary='true'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementObject mo in searcher.Get())
{
DateTime dt =
ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());

Console.WriteLine (dt);
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top