Determine system uptime

Posted on 2006-04-25
Last Modified: 2012-06-21
This is what is currently use:

    String Struptime;
    int tick = Environment.TickCount;
            int hour, min, sec,dag;

            hour = tick / (1000 * 60 * 60);
            tick = tick - (hour * (1000 * 60 * 60));
            min = tick / (1000 * 60);
            tick = tick - (min * (1000 * 60));
            sec = tick / (1000);
          tick = tick - (sec * (1000));
            dag = hour / 24;
            if (dag > 1) {

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0}d:{1}u:{2}m:{3}s:{4}ms",dag, hour, min, sec,tick);

But the  Environment.TickCount
[quote]The following code example demonstrates how to retrieve the positive range of values returned by the TickCount property. The TickCount property cycles between Int32.MinValue, which is a negative number, and Int32.MaxValue once every 49.8 days. This code sample removes the sign bit to yield a nonnegative number that cycles between zero and MaxValue once every 24.9 days.[/quote]

So this is not very usable to determine the servers uptime.

I could also use:

>net statistics server
Serverstatistieken voor \\
Statistieken vanaf 4/25/2006 8:56 AM

But this also isn't usable since you have to connect to the server and that is not possible. It should be some value i can read with an .aspx page.

Question by:markyvt
    LVL 8

    Accepted Solution

    this works

            private string GetUptime()
                string strComputer = "\\\\wkitw21";   //computername
                string perftimestamp = "";
                string perttimefreq = "";
                string counter = "";
                long iUptimeInSec = 0;

                 System.Management.ManagementScope oMs = new System.Management.ManagementScope(strComputer);

                 System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select * from Win32_PerfRawData_PerfOS_System");

                 //Execute the query  
                ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);

                //Get the results
                ManagementObjectCollection oReturnCollection = oSearcher.Get();

                 foreach (ManagementObject oReturn in oReturnCollection)
                     perftimestamp = oReturn["Timestamp_Object"].ToString();
                     perttimefreq = oReturn["Frequency_Object"].ToString();
                     counter = oReturn["SystemUpTime"].ToString();

                // Calculation in seconds:
                iUptimeInSec = Convert.ToInt64((double.Parse(perftimestamp) - double.Parse(counter)) / double.Parse(perttimefreq));

                // convert the seconds
                return ConvertTime(iUptimeInSec);

            private string ConvertTime(long seconds)
                long ConvSec;
                long ConvMin;
                long ConvHour;
                long ConvDays;

                Math.DivRem(seconds, 60, out ConvSec);

                Math.DivRem(seconds, 3600, out ConvMin);
                ConvMin = ConvMin / 60;
                Math.DivRem(seconds, (3600 * 24), out ConvHour);
                ConvHour = ConvHour / 3600;

                ConvDays = (seconds / (3600 * 24));

                return (ConvDays.ToString() + " days " + ConvHour.ToString() + " hours " + ConvMin.ToString() + " minutes " + ConvSec.ToString() + " seconds ");
    LVL 2

    Author Comment

    Error      1      The type or namespace name 'Management' does not exist in the namespace 'System' (are you missing an assembly reference?)      C:\Documents and Settings\Mark\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.cs      33      20      WindowsApplication1
    LVL 8

    Expert Comment

    you need to add a reference to
    LVL 2

    Author Comment

    which file should i reference then ?

    LVL 8

    Expert Comment

    right click on the project, click 'Add Reference'

    select System.Management.dll and click OK

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now