Solved

retrieving System Up time

Posted on 2004-07-30
6
312 Views
Last Modified: 2010-04-15
My code for some reason isn't working :P



                  PerformanceCounter pc = new PerformanceCounter();
                  pc.CategoryName = "System";
                  pc.CounterName = "System Up Time";                  

                  this.Label1.Text = pc.NextValue().ToString();


Object get's created just fine.. but it's saying that it can't find that category name... is there something else special I have to do with the performance counter to get it to work in this instance? also what permissions would I need to do this on a remote machine?
0
Comment
Question by:jayrod
  • 3
  • 3
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
PerformanceCounter pc = new PerformanceCounter("System","System Up Time");

You can write values only to counters that reside on the local computer. However, you can read counter values from any computer in the enterprise for which you have access privileges.

When you set the MachineName property to point to a remote computer, the PerformanceCounter instance attempts to open the counter on that computer. If the counter does not exist, setting this property throws an exception.

I would assume you would need permissions as your account to do it on the local machine which I believe is admin ... the windows guys would prob know more details ...

I beleive the overloaded constructor also defaults the InstanceName to PC.InstanceName="Explorer"; which the empty param constructor does not.
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
Nope still doesn't work...

            private void Page_Load(object sender, System.EventArgs e)
            {
                  PerformanceCounter pc = new PerformanceCounter("System","System Up Time");
                  this.Label1.Text = pc.NextValue().ToString();

}
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
Comment Utility
using System;
using System.Diagnostics;
namespace Uptime
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Uptime
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  PerformanceCounter pc = new PerformanceCounter("System","System Up Time");
                  pc.NextValue();
                  TimeSpan t = TimeSpan.FromSeconds(pc.NextValue());
                  Console.WriteLine(t.ToString());
            }
      }
}

1 small difference and im working fine ...
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Author Comment

by:jayrod
Comment Utility
possible difference between using a webpage and using a console app?  Actually now that I think about it I guess that might just be the problem as all requests from IIS are routed through the aspnet user.


hmm... wonder what I'd have to do to get this working thorugh a web page.
0
 
LVL 3

Author Comment

by:jayrod
Comment Utility
well.. I just did it using a console app... and it appears that I still get this exception

{"Category does not exist." }

When I go to admin tools on this machine and go to performance  I can't seem to add any counters there... could I maybe have a different problem?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
what are the specifics of the environment ?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

9 Experts available now in Live!

Get 1:1 Help Now