473,573 Members | 2,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12608
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.ne t> 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_PERFORMANC E_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 (NtQuerySystemI nformation with SystemProcessor Times
(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.Diagnost ics;

class App
{
static void Main()
{
PerformanceCoun ter pc = new PerformanceCoun ter("System",
"System Up Time");
pc.NextValue();
TimeSpan ts = TimeSpan.FromSe conds(pc.NextVa lue());
Console.WriteLi ne(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****@dylanpa rry.com> wrote in message
news:14******** *******@dylanpa rry.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.Manageme nt, following snip illustares
how...

using System.Manageme nt;
....

SelectQuery query = new SelectQuery("SE LECT LastBootUpTime FROM
Win32_Operating System WHERE Primary='true'" );
ManagementObjec tSearcher searcher = new ManagementObjec tSearcher(query );
foreach(Managem entObject mo in searcher.Get())
{
DateTime dt =
ManagementDateT imeConverter.To DateTime(mo.Pro perties["LastBootUpTime "].Value.ToString ());

Console.WriteLi ne (dt);
}

Willy.
Jun 19 '06 #5

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

Similar topics

8
5257
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
4126
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 are already running and how much memory they take. If this is off-topic, I would appreciate if you redirect me. Thank! B.
24
3074
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
2224
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 *sometimes* crashes upon starting. The progressbar you get when you manually start the server will very slowly progress, and when it hits 100% after...
4
1882
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 Directory domain... needs to be in C# or VB.NET preferably. thanks!
7
3085
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 nothing but keep track of elapsed time. The reason I need a service is because the IO tasks are login agnostic. It doesn't matter if anyone, or...
0
1651
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 $uptime_output = system("uptime", $uptime_output); print $uptime_output; ?>
1
1494
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 Webmail * Catch All Email Account * Java Server Pages (JSP)
8
2711
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 each are down on average one day out of ten. Use a failover DNS system with monitoring to switch servers if one fails. On average: One server...
0
7788
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7799
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8080
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1048
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.