編集

次の方法で共有


PerformanceCounter Constructors

Definition

Initializes a new instance of the PerformanceCounter class.

Overloads

PerformanceCounter()

Initializes a new, read-only instance of the PerformanceCounter class, without associating the instance with any system or custom performance counter.

PerformanceCounter(String, String)

Initializes a new, read-only 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 have a single instance.

PerformanceCounter(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.

PerformanceCounter(String, String, String)

Initializes a new, read-only instance of the PerformanceCounter class and associates it with the specified system or custom performance counter and category instance on the local computer.

PerformanceCounter(String, 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 and category instance on the local computer.

PerformanceCounter(String, String, String, String)

Initializes a new, read-only instance of the PerformanceCounter class and associates it with the specified system or custom performance counter and category instance, on the specified computer.

PerformanceCounter()

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

Initializes a new, read-only instance of the PerformanceCounter class, without associating the instance with any system or custom performance counter.

public:
 PerformanceCounter();
public PerformanceCounter ();
Public Sub New ()

Examples

The following code example creates a default instance of the PerformanceCounter class. After the instance is created, the CategoryName, CounterName, and InstanceName property values are set, and the results of a call to the NextValue method are displayed.

PerformanceCounter^ PC = gcnew PerformanceCounter;
PC->CategoryName = "Process";
PC->CounterName = "Private Bytes";
PC->InstanceName = "Explorer";
MessageBox::Show( PC->NextValue().ToString() );
PerformanceCounter performanceCounter = new PerformanceCounter();
performanceCounter.CategoryName = "Process";
performanceCounter.CounterName = "Private Bytes";
performanceCounter.InstanceName = "Explorer";
MessageBox.Show(performanceCounter.NextValue().ToString());
Dim PC As New PerformanceCounter()
PC.CategoryName = "Process"
PC.CounterName = "Private Bytes"
PC.InstanceName = "Explorer"
MessageBox.Show(PC.NextValue().ToString())

Remarks

This overload of the constructor sets the CategoryName, CounterName, and InstanceName properties to empty strings (""), and sets the MachineName property to the local computer, (".").

This constructor does not initialize the performance counter, so it does not associate the instance with an existing counter on the local computer. To point to a specific performance counter, set the CategoryName, CounterName, and, optionally, the InstanceName and MachineName properties before reading any other properties or attempting to read from a counter. To write to a performance counter, set the ReadOnly property to false.

Note

The HostProtectionAttribute attribute applied to this member has the following Resources property value: Synchronization | SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Applies to

PerformanceCounter(String, String)

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

Initializes a new, read-only 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 have a single instance.

public:
 PerformanceCounter(System::String ^ categoryName, System::String ^ counterName);
public PerformanceCounter (string categoryName, string counterName);
new System.Diagnostics.PerformanceCounter : string * string -> System.Diagnostics.PerformanceCounter
Public Sub New (categoryName As String, counterName As String)

Parameters

categoryName
String

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

counterName
String

The name of the performance counter.

Exceptions

categoryName is an empty string ("").

-or-

counterName is an empty string ("").

-or-

The category specified does not exist.

-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.

categoryName or counterName is null.

An error occurred when accessing a system API.

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

Remarks

The parameter strings are not case-sensitive.

Use this overload to access a 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 can access any read-only or read/write counter, but does so in a read-only mode. A PerformanceCounter instance created using this overload cannot write to the counter, even if the counter itself is read/write.

This overload of the constructor sets the CategoryName and CounterName 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.

Note

To read performance counters from a non-interactive logon session in Windows Vista and later, 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.

Applies to

PerformanceCounter(String, String, Boolean)

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

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.

public:
 PerformanceCounter(System::String ^ categoryName, System::String ^ counterName, bool readOnly);
public PerformanceCounter (string categoryName, string counterName, bool readOnly);
new System.Diagnostics.PerformanceCounter : string * string * bool -> System.Diagnostics.PerformanceCounter
Public Sub New (categoryName As String, counterName As String, readOnly As Boolean)

Parameters

categoryName
String

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

counterName
String

The name of the performance counter.

readOnly
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.

Exceptions

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.

categoryName or counterName is null.

An error occurred when accessing a system API.

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

Examples

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.

PC = gcnew PerformanceCounter( "AverageCounter64SampleCategory","AverageCounter64Sample",false );
avgCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory",
    "AverageCounter64Sample",
    false);
avgCounter64Sample = New PerformanceCounter("AverageCounter64SampleCategory", "AverageCounter64Sample", False)

Remarks

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.

Note

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.

Note

To read performance counters from a non-interactive logon session in Windows Vista and later, 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.

Applies to

PerformanceCounter(String, String, String)

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

Initializes a new, read-only instance of the PerformanceCounter class and associates it with the specified system or custom performance counter and category instance on the local computer.

public:
 PerformanceCounter(System::String ^ categoryName, System::String ^ counterName, System::String ^ instanceName);
public PerformanceCounter (string categoryName, string counterName, string instanceName);
new System.Diagnostics.PerformanceCounter : string * string * string -> System.Diagnostics.PerformanceCounter
Public Sub New (categoryName As String, counterName As String, instanceName As String)

Parameters

categoryName
String

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

counterName
String

The name of the performance counter.

instanceName
String

The name of the performance counter category instance, or an empty string (""), if the category contains a single instance.

Exceptions

categoryName is an empty string ("").

-or-

counterName is an empty string ("").

-or-

The category specified is not valid.

-or-

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

-or-

instanceName is longer than 127 characters.

-or-

categoryName and counterName have been localized into different languages.

categoryName or counterName is null.

An error occurred when accessing a system API.

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

Remarks

The parameter strings are not case-sensitive.

This overload of the constructor sets the CategoryName, CounterName, and InstanceName properties to the values you pass in, and sets the MachineName property to the local computer, ".".

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, CounterName, and InstanceName properties must point to an existing performance counter on the local computer. If the performance counter instance you point to is not valid, calling the constructor throws an exception.

This overload can access any read-only or read/write counter, but does so in a read-only mode. A PerformanceCounter instance created using this overload cannot write to the counter, even if the counter itself is read/write.

To create a performance category instance, specify an instanceName on the PerformanceCounter constructor. If the category instance specified by instanceName already exists the new object will reference the existing category instance.

Note

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.

Applies to

PerformanceCounter(String, String, String, Boolean)

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

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

public:
 PerformanceCounter(System::String ^ categoryName, System::String ^ counterName, System::String ^ instanceName, bool readOnly);
public PerformanceCounter (string categoryName, string counterName, string instanceName, bool readOnly);
new System.Diagnostics.PerformanceCounter : string * string * string * bool -> System.Diagnostics.PerformanceCounter
Public Sub New (categoryName As String, counterName As String, instanceName As String, readOnly As Boolean)

Parameters

categoryName
String

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

counterName
String

The name of the performance counter.

instanceName
String

The name of the performance counter category instance, or an empty string (""), if the category contains a single instance.

readOnly
Boolean

true to access a counter in read-only mode; false to access a counter in read/write mode.

Exceptions

categoryName is an empty string ("").

-or-

counterName is an empty string ("").

-or-

The read/write permission setting requested is invalid for this counter.

-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-

instanceName is longer than 127 characters.

-or-

categoryName and counterName have been localized into different languages.

categoryName or counterName is null.

An error occurred when accessing a system API.

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

Remarks

The parameter strings are not case-sensitive.

Use this overload to access a performance counter in either read-only or read/write mode.

This overload of the constructor sets the CategoryName, CounterName, and InstanceName properties to the values you pass in, it and sets the MachineName property to the local computer, ".".

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, CounterName, and InstanceName 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.

Note

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.

To create a performance category instance, specify an instanceName on the PerformanceCounter constructor. If the category instance specified by instanceName already exists the new object will reference the existing category instance.

Note

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.

Applies to

PerformanceCounter(String, String, String, String)

Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs
Source:
PerformanceCounter.cs

Initializes a new, read-only instance of the PerformanceCounter class and associates it with the specified system or custom performance counter and category instance, on the specified computer.

public:
 PerformanceCounter(System::String ^ categoryName, System::String ^ counterName, System::String ^ instanceName, System::String ^ machineName);
public PerformanceCounter (string categoryName, string counterName, string instanceName, string machineName);
new System.Diagnostics.PerformanceCounter : string * string * string * string -> System.Diagnostics.PerformanceCounter
Public Sub New (categoryName As String, counterName As String, instanceName As String, machineName As String)

Parameters

categoryName
String

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

counterName
String

The name of the performance counter.

instanceName
String

The name of the performance counter category instance, or an empty string (""), if the category contains a single instance.

machineName
String

The computer on which the performance counter and its associated category exist.

Exceptions

categoryName is an empty string ("").

-or-

counterName is an empty string ("").

-or-

The read/write permission setting requested is invalid for this counter.

-or-

The counter does not exist on the specified computer.

-or-

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

-or-

instanceName is longer than 127 characters.

-or-

categoryName and counterName have been localized into different languages.

The machineName parameter is not valid.

categoryName or counterName is null.

An error occurred when accessing a system API.

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

Remarks

The parameter strings are not case-sensitive.

This overload of the constructor sets the CategoryName, CounterName, InstanceName, and MachineName properties to the values you pass in.

This constructor initializes the performance counter and associates the instance with an existing counter (either a system or a custom counter) on the specified computer. The values that you pass in for the CategoryName, CounterName, InstanceName and MachineName properties must point to an existing performance counter. If the performance counter instance you point to is not valid, calling the constructor throws an exception. This overload can access any read-only or read/write counter, but does so in a read-only mode. A PerformanceCounter instance created using this overload cannot write to the counter, even if the counter itself is read/write.

Note

You cannot write to remote performance counters. There is no overload that allows you to specify a read/write instance of the PerformanceCounter class that connects to a remote computer.

To create a performance category instance, specify an instanceName on the PerformanceCounter constructor. If the category instance specified by instanceName already exists the new object will reference the existing category instance.

Note

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.

Note

In Windows Vista, when the remote computer is a member of a workgroup, you may need to disable UAC so that the local user account is not filtered and can be elevated to an administrator account. For security reasons, disabling UAC should be a last resort. For information on disabling UAC, see User Account Control and WMI.

Applies to