PerformanceCounterCategory.GetCounters Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengambil daftar penghitung dalam kategori penghitung kinerja ini.
Overload
GetCounters() |
Mengambil daftar penghitung dalam kategori penghitung kinerja yang berisi tepat satu instans. |
GetCounters(String) |
Mengambil daftar penghitung dalam kategori penghitung kinerja yang berisi satu atau beberapa instans. |
GetCounters()
Mengambil daftar penghitung dalam kategori penghitung kinerja yang berisi tepat satu instans.
public:
cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters();
public System.Diagnostics.PerformanceCounter[] GetCounters ();
member this.GetCounters : unit -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters () As PerformanceCounter()
Mengembalikan
Array PerformanceCounter objek yang menunjukkan penghitung yang terkait dengan kategori penghitung kinerja instans tunggal ini.
Pengecualian
Kategori ini bukan satu instans.
Panggilan ke API sistem yang mendasar gagal.
Kategori tidak memiliki instans terkait.
Kode yang dijalankan tanpa hak administratif mencoba membaca penghitung kinerja.
Contoh
Contoh kode berikut mendapatkan daftar PerformanceCounter objek dalam PerformanceCounterCategory. Ini pertama-tama PerformanceCounterCategory membuat dengan konstruktor yang sesuai, berdasarkan apakah nama komputer ditentukan. Kemudian menggunakan GetCounters metode untuk mengembalikan array PerformanceCounter objek, memilih GetCounters kelebihan beban berdasarkan apakah nama instans ditentukan.
Kelebihan beban ini GetCounters() gagal kecuali digunakan dengan kategori instans tunggal.
public:
static void Main(array<String^>^ args)
{
String^ categoryName = "";
String^ machineName = "";
String^ instanceName = "";
PerformanceCounterCategory^ pcc;
array<PerformanceCounter^>^ counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[1];
machineName = args[2]=="."? "": args[2];
instanceName = args[3];
}
catch (...)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName->Length>0)
{
pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = gcnew PerformanceCounterCategory(categoryName);
}
// Get the counters for this instance or a single instance
// of the selected category.
if (instanceName->Length > 0)
{
counters = pcc->GetCounters(instanceName);
}
else
{
counters = pcc->GetCounters();
}
}
catch (Exception^ ex)
{
Console::WriteLine("Unable to get counter information for " +
(instanceName->Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName->Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console::WriteLine(ex->Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters != nullptr)
{
Console::WriteLine("These counters exist in " +
(instanceName->Length > 0 ? "instance \"{1}\" of" : "single instance") +
" category {0} on " + (machineName->Length > 0 ? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX = 0; objX < counters->Length; objX++)
{
Console::WriteLine("{0,4} - {1}", objX + 1, counters[objX]->CounterName);
}
}
}
public static void Main(string[] args)
{
string categoryName = "";
string machineName = "";
string instanceName = "";
PerformanceCounterCategory pcc;
PerformanceCounter[] counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
machineName = args[1]=="."? "": args[1];
instanceName = args[2];
}
catch
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName.Length>0)
{
pcc = new PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = new PerformanceCounterCategory(categoryName);
}
// Get the counters for this instance or a single instance
// of the selected category.
if (instanceName.Length>0)
{
counters = pcc.GetCounters(instanceName);
}
else
{
counters = pcc.GetCounters();
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to get counter information for " +
(instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console.WriteLine(ex.Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters!=null)
{
Console.WriteLine("These counters exist in " +
(instanceName.Length>0? "instance \"{1}\" of": "single instance") +
" category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX=0; objX<counters.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
}
}
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim machineName As String = ""
Dim instanceName As String = ""
Dim pcc As PerformanceCounterCategory
Dim counters() As PerformanceCounter
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
machineName = IIf(args(1) = ".", "", args(1))
instanceName = args(2)
Catch ex As Exception
' Ignore the exception from non-supplied arguments.
End Try
Try
' Create the appropriate PerformanceCounterCategory object.
If machineName.Length > 0 Then
pcc = New PerformanceCounterCategory(categoryName, machineName)
Else
pcc = New PerformanceCounterCategory(categoryName)
End If
' Get the counters for this instance or a single instance
' of the selected category.
If instanceName.Length > 0 Then
counters = pcc.GetCounters(instanceName)
Else
counters = pcc.GetCounters()
End If
Catch ex As Exception
Console.WriteLine("Unable to get counter information for " & _
IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
"single-instance ") & "category ""{0}"" on " & _
IIf(machineName.Length > 0, "computer ""{1}"":", _
"this computer:"), _
categoryName, machineName, instanceName)
Console.WriteLine(ex.Message)
Return
End Try
' Display the counter names if GetCounters was successful.
If Not counters Is Nothing Then
Console.WriteLine("These counters exist in " & _
IIf(instanceName.Length > 0, "instance ""{1}"" of", _
"single instance") & " category {0} on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"":", "this computer:"), _
categoryName, instanceName, machineName)
' Display a numbered list of the counter names.
Dim objX As Integer
For objX = 0 To counters.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, _
counters(objX).CounterName)
Next objX
End If
End Sub
Keterangan
Untuk informasi selengkapnya tentang instans objek performa, lihat PerformanceCounter gambaran umum kelas.
Catatan
Untuk membaca penghitung kinerja dari sesi masuk non-interaktif di Windows Vista dan yang lebih baru, Windows XP Professional x64 Edition, atau Windows Server 2003, Anda harus menjadi anggota grup Pengguna Monitor Performa atau memiliki hak administratif.
Untuk menghindari harus meningkatkan hak istimewa Anda untuk mengakses penghitung kinerja di Windows Vista dan yang lebih baru, tambahkan diri Anda ke grup Pengguna Monitor Performa.
Di Windows Vista dan yang lebih baru, Kontrol Akun Pengguna (User Account Control atau UAC) menentukan hak istimewa pengguna. Jika Anda adalah anggota grup Administrator Bawaan, Anda diberi dua token akses run-time: token akses pengguna standar dan token akses administrator. Secara default, Anda berada dalam peran pengguna standar. Untuk menjalankan kode yang mengakses penghitung kinerja, Anda harus terlebih dahulu meningkatkan hak istimewa Anda dari pengguna standar ke administrator. Anda dapat melakukan ini saat memulai aplikasi dengan mengeklik kanan ikon aplikasi dan menunjukkan bahwa Anda ingin menjalankan sebagai administrator.
Lihat juga
Berlaku untuk
GetCounters(String)
Mengambil daftar penghitung dalam kategori penghitung kinerja yang berisi satu atau beberapa instans.
public:
cli::array <System::Diagnostics::PerformanceCounter ^> ^ GetCounters(System::String ^ instanceName);
public System.Diagnostics.PerformanceCounter[] GetCounters (string instanceName);
member this.GetCounters : string -> System.Diagnostics.PerformanceCounter[]
Public Function GetCounters (instanceName As String) As PerformanceCounter()
Parameter
- instanceName
- String
Instans objek performa untuk mengambil daftar penghitung terkait.
Mengembalikan
Array PerformanceCounter objek yang menunjukkan penghitung yang terkait dengan instans objek yang ditentukan dari kategori penghitung kinerja ini.
Pengecualian
Parameternya instanceName
adalah null
.
Properti CategoryName untuk instans ini PerformanceCounterCategory belum ditetapkan.
-atau-
Kategori tidak berisi instans yang ditentukan oleh instanceName
parameter .
Panggilan ke API sistem yang mendasar gagal.
Kode yang dijalankan tanpa hak administratif mencoba membaca penghitung kinerja.
Contoh
Contoh kode berikut mendapatkan daftar PerformanceCounter objek dalam PerformanceCounterCategory. Ini pertama-tama PerformanceCounterCategory membuat dengan konstruktor yang sesuai, berdasarkan apakah nama komputer ditentukan. Kemudian menggunakan GetCounters metode untuk mengembalikan array PerformanceCounter objek, memilih GetCounters kelebihan beban berdasarkan apakah nama instans ditentukan.
Kelebihan beban ini GetCounters(String) gagal kecuali digunakan dengan kategori yang berisi instans.
public:
static void Main(array<String^>^ args)
{
String^ categoryName = "";
String^ machineName = "";
String^ instanceName = "";
PerformanceCounterCategory^ pcc;
array<PerformanceCounter^>^ counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[1];
machineName = args[2]=="."? "": args[2];
instanceName = args[3];
}
catch (...)
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName->Length>0)
{
pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = gcnew PerformanceCounterCategory(categoryName);
}
// Get the counters for this instance or a single instance
// of the selected category.
if (instanceName->Length > 0)
{
counters = pcc->GetCounters(instanceName);
}
else
{
counters = pcc->GetCounters();
}
}
catch (Exception^ ex)
{
Console::WriteLine("Unable to get counter information for " +
(instanceName->Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName->Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console::WriteLine(ex->Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters != nullptr)
{
Console::WriteLine("These counters exist in " +
(instanceName->Length > 0 ? "instance \"{1}\" of" : "single instance") +
" category {0} on " + (machineName->Length > 0 ? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX = 0; objX < counters->Length; objX++)
{
Console::WriteLine("{0,4} - {1}", objX + 1, counters[objX]->CounterName);
}
}
}
public static void Main(string[] args)
{
string categoryName = "";
string machineName = "";
string instanceName = "";
PerformanceCounterCategory pcc;
PerformanceCounter[] counters;
// Copy the supplied arguments into the local variables.
try
{
categoryName = args[0];
machineName = args[1]=="."? "": args[1];
instanceName = args[2];
}
catch
{
// Ignore the exception from non-supplied arguments.
}
try
{
// Create the appropriate PerformanceCounterCategory object.
if (machineName.Length>0)
{
pcc = new PerformanceCounterCategory(categoryName, machineName);
}
else
{
pcc = new PerformanceCounterCategory(categoryName);
}
// Get the counters for this instance or a single instance
// of the selected category.
if (instanceName.Length>0)
{
counters = pcc.GetCounters(instanceName);
}
else
{
counters = pcc.GetCounters();
}
}
catch(Exception ex)
{
Console.WriteLine("Unable to get counter information for " +
(instanceName.Length>0? "instance \"{2}\" in ": "single-instance ") +
"category \"{0}\" on " + (machineName.Length>0? "computer \"{1}\":": "this computer:"),
categoryName, machineName, instanceName);
Console.WriteLine(ex.Message);
return;
}
// Display the counter names if GetCounters was successful.
if (counters!=null)
{
Console.WriteLine("These counters exist in " +
(instanceName.Length>0? "instance \"{1}\" of": "single instance") +
" category {0} on " + (machineName.Length>0? "computer \"{2}\":": "this computer:"),
categoryName, instanceName, machineName);
// Display a numbered list of the counter names.
int objX;
for(objX=0; objX<counters.Length; objX++)
{
Console.WriteLine("{0,4} - {1}", objX+1, counters[objX].CounterName);
}
}
}
Sub Main(ByVal args() As String)
Dim categoryName As String = ""
Dim machineName As String = ""
Dim instanceName As String = ""
Dim pcc As PerformanceCounterCategory
Dim counters() As PerformanceCounter
' Copy the supplied arguments into the local variables.
Try
categoryName = args(0)
machineName = IIf(args(1) = ".", "", args(1))
instanceName = args(2)
Catch ex As Exception
' Ignore the exception from non-supplied arguments.
End Try
Try
' Create the appropriate PerformanceCounterCategory object.
If machineName.Length > 0 Then
pcc = New PerformanceCounterCategory(categoryName, machineName)
Else
pcc = New PerformanceCounterCategory(categoryName)
End If
' Get the counters for this instance or a single instance
' of the selected category.
If instanceName.Length > 0 Then
counters = pcc.GetCounters(instanceName)
Else
counters = pcc.GetCounters()
End If
Catch ex As Exception
Console.WriteLine("Unable to get counter information for " & _
IIf(instanceName.Length > 0, "instance ""{2}"" in ", _
"single-instance ") & "category ""{0}"" on " & _
IIf(machineName.Length > 0, "computer ""{1}"":", _
"this computer:"), _
categoryName, machineName, instanceName)
Console.WriteLine(ex.Message)
Return
End Try
' Display the counter names if GetCounters was successful.
If Not counters Is Nothing Then
Console.WriteLine("These counters exist in " & _
IIf(instanceName.Length > 0, "instance ""{1}"" of", _
"single instance") & " category {0} on " & _
IIf(machineName.Length > 0, _
"computer ""{2}"":", "this computer:"), _
categoryName, instanceName, machineName)
' Display a numbered list of the counter names.
Dim objX As Integer
For objX = 0 To counters.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, _
counters(objX).CounterName)
Next objX
End If
End Sub
Keterangan
Untuk mewakili kategori instans tunggal, berikan string kosong ("") untuk instanceName
parameter .
Untuk informasi selengkapnya tentang instans objek performa, lihat PerformanceCounter gambaran umum kelas.
Catatan
Untuk membaca penghitung kinerja dari sesi masuk non-interaktif di Windows Vista dan yang lebih baru, Windows XP Professional x64 Edition, atau Windows Server 2003, Anda harus menjadi anggota grup Pengguna Monitor Performa atau memiliki hak administratif.
Untuk menghindari harus meningkatkan hak istimewa Anda untuk mengakses penghitung kinerja di Windows Vista dan yang lebih baru, tambahkan diri Anda ke grup Pengguna Monitor Performa.
Di Windows Vista dan yang lebih baru, Kontrol Akun Pengguna (User Account Control atau UAC) menentukan hak istimewa pengguna. Jika Anda adalah anggota grup Administrator Bawaan, Anda diberi dua token akses run-time: token akses pengguna standar dan token akses administrator. Secara default, Anda berada dalam peran pengguna standar. Untuk menjalankan kode yang mengakses penghitung kinerja, Anda harus terlebih dahulu meningkatkan hak istimewa Anda dari pengguna standar ke administrator. Anda dapat melakukan ini saat memulai aplikasi dengan mengeklik kanan ikon aplikasi dan menunjukkan bahwa Anda ingin menjalankan sebagai administrator.