Link to home
Start Free TrialLog in
Avatar of jayrod
jayrodFlag for United States of America

asked on

retrieving System Up time

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?
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

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.
Avatar of jayrod

ASKER

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();

}
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jayrod

ASKER

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.
Avatar of jayrod

ASKER

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?
what are the specifics of the environment ?