Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

retrieving System Up time

Posted on 2004-07-30
6
Medium Priority
?
368 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
ID: 11681243
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
ID: 11681350
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 2000 total points
ID: 11681451
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:jayrod
ID: 11682593
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
ID: 11682616
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
ID: 11685017
what are the specifics of the environment ?
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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