Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

retrieving System Up time

Posted on 2004-07-30
6
Medium Priority
?
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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