473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Windows uptime

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
Jun 19 '06 #1
4 12565
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
Jun 19 '06 #2
Vadym Stetsyak wrote:
Win32 function GetTickCount() returns the time in millis elapsed since system start;


Thanks!

--
Dylan Parry
http://webpageworkshop.co.uk -- FREE Web tutorials and references
Jun 19 '06 #3
"Vadym Stetsyak" <va*****@ukr.net> wrote:
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/...42eeac2963d4fa
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

--
http://barrkel.blogspot.com/
Jun 19 '06 #4

"Dylan Parry" <us****@dylanparry.com> wrote in message
news:14***************@dylanparry.com...
| 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.Properti es["LastBootUpTime"].Value.ToString());

Console.WriteLine (dt);
}

Willy.
Jun 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Oli Schwarz | last post by:
Hello, how can I read out the uptime of a unix system in python (Linux and *BSD). I have not found a uptime-function in the Library. Regards Oli
3
by: Bolin | last post by:
I was wondering how to get some system info from a PC running Windows in python. I am especially interested in knowing how much RAM the computer has, how much diskspace is still left, and what jobs...
24
by: Esmail Bonakdarian | last post by:
Hi, Is there a way to display how long a Win XP system has been up? Somewhat analogous to the *nix uptime command. Thanks, Esmail
2
by: Razzie | last post by:
Hey all, I wrote a Windows Service. When I test it on my developement machine (winXP) it works fine. It starts ok, never crashes, etc. When I install the service on another machine (win2000) it...
4
by: Brian Henry | last post by:
does anyone know of an easy way to say how long has a specific system been up and running? I just need to get back the uptime of the servers on our network for a report, they are all in a Active...
7
by: deko | last post by:
I'm trying to finalize a windows service design any would appreciate any comments - I'm new to windows services. Essentially my UI app relies on a service to schedule IO tasks. The service does...
0
by: streamkid | last post by:
hello.. could anyone please help me build a uptime script for openbsd? i tried phpsysinfo, it doesn't work (only uptime works, and that's around 15k days :p) i tried these: <?php...
1
by: karen.systems | last post by:
WOLWER offers new web hosting packages ==================================================== Unix Web Hosting * Unix Virtual Private Hosting * 5 GB to 500 GB Web Space * Sub Domains, POP3 and...
8
by: Guy Macon | last post by:
How to increase web server uptime with DNS failover: For illustration, let's start with some really bad hosting... Find four free web hosts that are each 90% reliable -- in other words they...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.