Bagikan melalui


PerformanceCounterCategory.CounterExists Metode

Definisi

Menentukan apakah penghitung tertentu didaftarkan ke kategori tertentu.

Overload

CounterExists(String)

Menentukan apakah penghitung yang ditentukan terdaftar untuk kategori ini, yang ditunjukkan oleh CategoryName properti dan MachineName .

CounterExists(String, String)

Menentukan apakah penghitung yang ditentukan didaftarkan ke kategori yang ditentukan pada komputer lokal.

CounterExists(String, String, String)

Menentukan apakah penghitung yang ditentukan didaftarkan ke kategori yang ditentukan pada komputer jarak jauh.

CounterExists(String)

Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs

Menentukan apakah penghitung yang ditentukan terdaftar untuk kategori ini, yang ditunjukkan oleh CategoryName properti dan MachineName .

public:
 bool CounterExists(System::String ^ counterName);
public bool CounterExists (string counterName);
member this.CounterExists : string -> bool
Public Function CounterExists (counterName As String) As Boolean

Parameter

counterName
String

Nama penghitung kinerja yang akan dicari.

Mengembalikan

true jika penghitung didaftarkan ke kategori yang ditentukan oleh CategoryName properti dan MachineName ; jika tidak, false.

Pengecualian

counterName adalah null.

Properti CategoryName belum disetel.

Panggilan ke API sistem yang mendasar gagal.

Kode yang dijalankan tanpa hak istimewa administratif mencoba membaca penghitung kinerja.

Contoh

Contoh kode berikut menentukan apakah PerformanceCounter ada. Ini mendapatkan nama kategori, nama penghitung, dan nama komputer dari baris perintah, jika diberikan. Ini membuat PerformanceCounterCategory objek menggunakan yang sesuai PerformanceCounterCategory. Kemudian menggunakan CounterExists(String) metode untuk menentukan apakah yang ditentukan PerformanceCounter ada, dan memberi tahu pengguna.

public static void Main(string[] args)
{
    string categoryName = "";
    string counterName = "";
    string machineName = "";
    bool objectExists = false;
    PerformanceCounterCategory pcc;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        counterName = args[1];
        machineName = (args[2]=="."? "": args[2]);
    }
    catch(Exception ex)
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        if (machineName.Length==0)
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }

        // Check whether the specified counter exists.
        // Use the per-instance overload of CounterExists.
        objectExists = pcc.CounterExists(counterName);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "counter \"{0}\" in category \"{1}\" on "+
            (machineName.Length>0? "computer \"{2}\".": "this computer.")+ "\n" +
            ex.Message, counterName, categoryName, machineName);
        return;
    }

    // Tell the user whether the counter exists.
    Console.WriteLine("Counter \"{0}\" " + (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        counterName, pcc.CategoryName, pcc.MachineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False
    Dim pcc As PerformanceCounterCategory

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        counterName = args(1)
        machineName = IIf(args(2) = ".", "", args(2))
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        If machineName.Length = 0 Then
            pcc = New PerformanceCounterCategory(categoryName)
        Else
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        End If

        ' Check whether the specified counter exists.
        ' Use the per-instance overload of CounterExists.
        objectExists = pcc.CounterExists(counterName)

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, pcc.CategoryName, pcc.MachineName)
End Sub

Keterangan

Anda harus mengatur CategoryName properti sebelum memanggil metode ini. Jika tidak, pengecualian ditampilkan.

Jika Anda belum mengatur MachineName properti , metode ini menggunakan komputer lokal (".").

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

CounterExists(String, String)

Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs

Menentukan apakah penghitung yang ditentukan didaftarkan ke kategori yang ditentukan pada komputer lokal.

public:
 static bool CounterExists(System::String ^ counterName, System::String ^ categoryName);
public static bool CounterExists (string counterName, string categoryName);
static member CounterExists : string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String) As Boolean

Parameter

counterName
String

Nama penghitung kinerja yang akan dicari.

categoryName
String

Nama kategori penghitung kinerja, atau objek performa, yang terkait dengan penghitung kinerja yang ditentukan.

Mengembalikan

true, jika penghitung didaftarkan ke kategori yang ditentukan di komputer lokal; jika tidak, false.

Pengecualian

categoryName adalah null.

-atau-

counterName adalah null.

categoryName adalah string kosong ("").

Nama kategori tidak ada.

Panggilan ke API sistem yang mendasar gagal.

Kode yang dijalankan tanpa hak istimewa administratif mencoba membaca penghitung kinerja.

Contoh

Contoh kode berikut menentukan apakah PerformanceCounter ada. Ini mendapatkan nama kategori, nama penghitung, dan nama komputer dari baris perintah, jika diberikan. Ini menggunakan kelebihan beban statis metode CounterExists untuk menentukan apakah nama yang ditentukan PerformanceCounter ada di PerformanceCounterCategory. Kelebihan beban dipilih berdasarkan apakah nama komputer disediakan.

public static void Main(string[] args)
{
    string categoryName = "";
    string counterName = "";
    string machineName = "";
    bool objectExists = false;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        counterName = args[1];
        machineName = args[2]=="."? "": args[2];
    }
    catch(Exception ex)
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // Check whether the specified counter exists.
        // Use the static forms of the CounterExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "counter \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\".": "this computer.") + "\n" +
            ex.Message, counterName, categoryName, machineName);
        return;
    }

    // Tell the user whether the counter exists.
    Console.WriteLine("Counter \"{0}\" "+ (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        counterName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        counterName = args(1)
        machineName = IIf(args(2) = ".", "", args(2))
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Check whether the specified counter exists.
        ' Use the static forms of the CounterExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, categoryName, machineName)
End Sub

Keterangan

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

CounterExists(String, String, String)

Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs
Sumber:
PerformanceCounterCategory.cs

Menentukan apakah penghitung yang ditentukan didaftarkan ke kategori yang ditentukan pada komputer jarak jauh.

public:
 static bool CounterExists(System::String ^ counterName, System::String ^ categoryName, System::String ^ machineName);
public static bool CounterExists (string counterName, string categoryName, string machineName);
static member CounterExists : string * string * string -> bool
Public Shared Function CounterExists (counterName As String, categoryName As String, machineName As String) As Boolean

Parameter

counterName
String

Nama penghitung kinerja yang akan dicari.

categoryName
String

Nama kategori penghitung kinerja, atau objek performa, yang terkait dengan penghitung kinerja yang ditentukan.

machineName
String

Nama komputer tempat kategori penghitung kinerja dan penghitung terkait ada.

Mengembalikan

true, jika penghitung didaftarkan ke kategori yang ditentukan pada komputer yang ditentukan; jika tidak, false.

Pengecualian

categoryName adalah null.

-atau-

counterName adalah null.

categoryName adalah string kosong ("").

-atau-

tidak machineName valid.

Nama kategori tidak ada.

Panggilan ke API sistem yang mendasar gagal.

Kode yang dijalankan tanpa hak istimewa administratif mencoba membaca penghitung kinerja.

Contoh

Contoh kode berikut menentukan apakah PerformanceCounter ada. Ini mendapatkan nama kategori, nama penghitung, dan nama komputer dari baris perintah, jika diberikan. Ini menggunakan kelebihan beban statis metode CounterExists untuk menentukan apakah nama yang ditentukan PerformanceCounter ada di PerformanceCounterCategory. Kelebihan beban dipilih berdasarkan apakah nama komputer disediakan.

public static void Main(string[] args)
{
    string categoryName = "";
    string counterName = "";
    string machineName = "";
    bool objectExists = false;

    // Copy the supplied arguments into the local variables.
    try
    {
        categoryName = args[0];
        counterName = args[1];
        machineName = args[2]=="."? "": args[2];
    }
    catch(Exception ex)
    {
        // Ignore the exception from non-supplied arguments.
    }

    try
    {
        // Check whether the specified counter exists.
        // Use the static forms of the CounterExists method.
        if (machineName.Length==0)
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName);
        }
        else
        {
            objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName, machineName);
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to check for the existence of " +
            "counter \"{0}\" in category \"{1}\" on " +
            (machineName.Length>0? "computer \"{2}\".": "this computer.") + "\n" +
            ex.Message, counterName, categoryName, machineName);
        return;
    }

    // Tell the user whether the counter exists.
    Console.WriteLine("Counter \"{0}\" "+ (objectExists? "exists": "does not exist") +
        " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."),
        counterName, categoryName, machineName);
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim counterName As String = ""
    Dim machineName As String = ""
    Dim objectExists As Boolean = False

    ' Copy the supplied arguments into the local variables.
    Try
        categoryName = args(0)
        counterName = args(1)
        machineName = IIf(args(2) = ".", "", args(2))
    Catch ex As Exception
        ' Ignore the exception from non-supplied arguments.
    End Try

    Try
        ' Check whether the specified counter exists.
        ' Use the static forms of the CounterExists method.
        If machineName.Length = 0 Then
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName)
        Else
            objectExists = PerformanceCounterCategory.CounterExists( _
                counterName, categoryName, machineName)
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to check for the existence of " & _
            "counter ""{0}"" in category ""{1}"" on " & _
            IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer.") & vbCrLf & _
            ex.Message, counterName, categoryName, machineName)
        Return
    End Try

    ' Tell the user whether the counter exists.
    Console.WriteLine("Counter ""{0}"" " & _
        IIf(objectExists, "exists", "does not exist") & _
        " in category ""{1}"" on " & _
        IIf(machineName.Length > 0, _
            "computer ""{2}"".", "this computer."), _
        counterName, categoryName, machineName)
End Sub

Keterangan

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