Determine system uptime

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) {
            hour=hour-(24*dag);
            }

            StringBuilder sb = new StringBuilder();

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

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:

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

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.

LVL 2
markyvtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rfgkevCommented:
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 ");
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
markyvtAuthor Commented:
[quote]
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
[/quote]
0
rfgkevCommented:
you need to add a reference to system.management
0
markyvtAuthor Commented:
which file should i reference then ?

0
rfgkevCommented:
right click on the project, click 'Add Reference'

select System.Management.dll and click OK
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.