Solved

retrieving System Up time

Posted on 2004-07-30
6
323 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 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Events in static methods 3 50
C# TextBox 11 30
Counting connections to SQL Server through C# 3 29
Web Form VB.Net  import CSV 4 27
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

15 Experts available now in Live!

Get 1:1 Help Now