• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 837
  • Last Modified:

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.

0
markyvt
Asked:
markyvt
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now