Export (0) Print
Expand All

PerformanceCounter Constructor (String, String, Boolean)

Initializes a new, read-only or read/write instance of the PerformanceCounter class and associates it with the specified system or custom performance counter on the local computer. This constructor requires that the category contain a single instance.

Namespace:  System.Diagnostics
Assembly:  System (in System.dll)

public PerformanceCounter(
	string categoryName,
	string counterName,
	bool readOnly
)

Parameters

categoryName
Type: System.String

The name of the performance counter category (performance object) with which this performance counter is associated.

counterName
Type: System.String

The name of the performance counter.

readOnly
Type: System.Boolean

true to access the counter in read-only mode (although the counter itself could be read/write); false to access the counter in read/write mode.

ExceptionCondition
InvalidOperationException

The categoryName is an empty string ("").

-or-

The counterName is an empty string ("").

-or-

The category specified does not exist. (if readOnly is true).

-or-

The category specified is not a .NET Framework custom category (if readOnly is false).

-or-

The category specified is marked as multi-instance and requires the performance counter to be created with an instance name.

-or-

categoryName and counterName have been localized into different languages.

ArgumentNullException

categoryName or counterName is null.

Win32Exception

An error occurred when accessing a system API.

PlatformNotSupportedException

The platform is Windows 98 or Windows Millennium Edition (Me), which does not support performance counters.

UnauthorizedAccessException

Code that is executing without administrative privileges attempted to read a performance counter.

The parameter strings are not case-sensitive.

Use this overload to access a read-only or read/write counter on the local computer that belongs to a category containing a single performance counter category instance. If you attempt to use this constructor to point this PerformanceCounter instance to a category that contains multiple instances, the constructor throws an exception.

This overload of the constructor sets the CategoryName, CounterName, and ReadOnly properties to the values you pass in, sets the MachineName property to the local computer, ".", and sets the InstanceName property to an empty string ("").

This constructor initializes the performance counter and associates the instance with an existing counter (either a system or a custom counter) on the local computer. The values that you pass in for the CategoryName and CounterName properties must point to an existing performance counter on the local computer. If the performance counter instance that you point to is not valid, calling the constructor throws an exception.

NoteNote

You can use this overload to connect to a system counter, but you cannot write to a system counter. Therefore, setting readOnly to false when connecting to a system counter causes the constructor to throw an exception.

NoteNote

To read performance counters in Windows Vista, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

To avoid having to elevate your privileges to access performance counters in Windows Vista, add yourself to the Performance Monitor Users group.

In Windows Vista, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

The following code example creates an instance of the PerformanceCounter class. The example passes in category names, counter names, and a flag value indicating that the counter is not read-only. This code example is part of a larger example for the PerformanceCounter class.

		avgCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory", 
			"AverageCounter64Sample", 
			false);
		

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft