473,699 Members | 2,664 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 12629
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
5262
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
4134
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
3092
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
2227
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 2 minutes or something I get the error 'The service could not be started because it did not...
4
1887
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
3086
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 no one, is logged in. The IO tasks are defined in the UI.
0
1653
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
1503
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
2719
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 will be down one day out of ten
0
8704
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8939
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5879
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.