PerformanceCounterCategory.CounterExists Metoda

Definicja

Określa, czy określony licznik jest zarejestrowany w określonej kategorii.

Przeciążenia

CounterExists(String)

Określa, czy określony licznik jest zarejestrowany w tej kategorii, który jest wskazywany przez CategoryName właściwości i MachineName .

CounterExists(String, String)

Określa, czy określony licznik jest zarejestrowany w określonej kategorii na komputerze lokalnym.

CounterExists(String, String, String)

Określa, czy określony licznik jest zarejestrowany w określonej kategorii na komputerze zdalnym.

CounterExists(String)

Określa, czy określony licznik jest zarejestrowany w tej kategorii, który jest wskazywany przez CategoryName właściwości i 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

Parametry

counterName
String

Nazwa licznika wydajności do wyszukania.

Zwraca

Boolean

true jeśli licznik jest zarejestrowany w kategorii określonej przez CategoryName właściwości i MachineName ; w przeciwnym razie false.

Wyjątki

Wartość counterName to null.

Właściwość nie została ustawiona CategoryName .

Wywołanie podstawowego interfejsu API systemu nie powiodło się.

Kod wykonywany bez uprawnień administracyjnych, który próbował odczytać licznik wydajności.

Przykłady

Poniższy przykład kodu określa, czy istnieje PerformanceCounter . Pobiera nazwę kategorii, nazwę licznika i nazwę komputera z wiersza polecenia, jeśli są one podane. PerformanceCounterCategory Tworzy obiekt przy użyciu odpowiedniego PerformanceCounterCategoryobiektu . Następnie używa CounterExists(String) metody , aby określić, czy określony PerformanceCounter istnieje, i informuje użytkownika.

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

Uwagi

Przed wywołaniem tej metody należy ustawić CategoryName właściwość . W przeciwnym razie jest zgłaszany wyjątek.

Jeśli właściwość nie jest ustawiona MachineName , ta metoda używa komputera lokalnego (".").

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w Windows Vista i nowszych, Windows XP Professional x64 Edition lub Windows Server 2003, musisz być członkiem grupy użytkownicy monitor wydajności lub mieć uprawnienia administracyjne.

Aby uniknąć konieczności podniesienia uprawnień dostępu do liczników wydajności w Windows Vista i nowszych, dodaj się do grupy użytkownicy monitor wydajności.

W systemie Windows Vista i nowszych Kontrola konta użytkownika (UAC) określa uprawnienia użytkownika. Jeśli jesteś członkiem wbudowanej grupy Administratorzy, masz przypisane dwa tokeny dostępu w czasie wykonywania: token dostępu użytkownika standardowego i token dostępu administratora. Domyślnie jesteś w roli użytkownika standardowego. Aby wykonać kod, który uzyskuje dostęp do liczników wydajności, musisz najpierw podnieść poziom uprawnień od użytkownika standardowego do administratora. Możesz to zrobić po uruchomieniu aplikacji, klikając prawym przyciskiem myszy ikonę aplikacji i wskazując, że chcesz uruchomić jako administrator.

Zobacz też

Dotyczy

CounterExists(String, String)

Określa, czy określony licznik jest zarejestrowany w określonej kategorii na komputerze lokalnym.

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

Parametry

counterName
String

Nazwa licznika wydajności do wyszukania.

categoryName
String

Nazwa kategorii licznika wydajności lub obiektu wydajności, z którym jest skojarzony określony licznik wydajności.

Zwraca

Boolean

true, jeśli licznik jest zarejestrowany w określonej kategorii na komputerze lokalnym; w przeciwnym razie , false.

Wyjątki

Wartość categoryName to null.

-lub- Wartość counterName to null.

Jest categoryName to pusty ciąg ("").

Nazwa kategorii nie istnieje.

Wywołanie podstawowego interfejsu API systemu nie powiodło się.

Kod wykonywany bez uprawnień administracyjnych, który próbował odczytać licznik wydajności.

Przykłady

Poniższy przykład kodu określa, czy istnieje PerformanceCounter . Pobiera nazwę kategorii, nazwę licznika i nazwę komputera z wiersza polecenia, jeśli zostały podane. Używa ona statycznych przeciążeń CounterExists metody w celu określenia, czy określona PerformanceCounter nazwa istnieje w obiekcie PerformanceCounterCategory. Przeciążenie jest wybierane na podstawie tego, czy podano nazwę komputera.

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

Uwagi

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w Windows Vista i nowszych, Windows XP Professional x64 Edition lub Windows Server 2003, musisz być członkiem grupy użytkowników monitor wydajności lub mieć uprawnienia administracyjne.

Aby uniknąć konieczności podniesienia uprawnień dostępu do liczników wydajności w Windows Vista i nowszych, dodaj się do grupy użytkownicy monitor wydajności.

W systemie Windows Vista i nowszych Kontrola konta użytkownika (UAC) określa uprawnienia użytkownika. Jeśli jesteś członkiem wbudowanej grupy Administratorzy, masz przypisane dwa tokeny dostępu w czasie wykonywania: token dostępu użytkownika standardowego i token dostępu administratora. Domyślnie jesteś w roli użytkownika standardowego. Aby wykonać kod, który uzyskuje dostęp do liczników wydajności, należy najpierw podnieść poziom uprawnień od użytkownika standardowego do administratora. Możesz to zrobić po uruchomieniu aplikacji, klikając prawym przyciskiem myszy ikonę aplikacji i wskazując, że chcesz uruchomić jako administrator.

Zobacz też

Dotyczy

CounterExists(String, String, String)

Określa, czy określony licznik jest zarejestrowany w określonej kategorii na komputerze zdalnym.

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

Parametry

counterName
String

Nazwa licznika wydajności do wyszukania.

categoryName
String

Nazwa kategorii licznika wydajności lub obiektu wydajności, z którym jest skojarzony określony licznik wydajności.

machineName
String

Nazwa komputera, na którym istnieje kategoria licznika wydajności i skojarzone z nim liczniki.

Zwraca

Boolean

true, jeśli licznik jest zarejestrowany w określonej kategorii na określonym komputerze; w przeciwnym razie , false.

Wyjątki

Wartość categoryName to null.

-lub- Wartość counterName to null.

Jest categoryName to pusty ciąg ("").

-lub- Element machineName jest nieprawidłowy.

Nazwa kategorii nie istnieje.

Wywołanie podstawowego interfejsu API systemu nie powiodło się.

Kod wykonywany bez uprawnień administracyjnych, który próbował odczytać licznik wydajności.

Przykłady

Poniższy przykład kodu określa, czy istnieje PerformanceCounter . Pobiera nazwę kategorii, nazwę licznika i nazwę komputera z wiersza polecenia, jeśli zostały podane. Używa ona statycznych przeciążeń CounterExists metody w celu określenia, czy określona PerformanceCounter nazwa istnieje w obiekcie PerformanceCounterCategory. Przeciążenie jest wybierane na podstawie tego, czy podano nazwę komputera.

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

Uwagi

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w Windows Vista i nowszych, Windows XP Professional x64 Edition lub Windows Server 2003, musisz być członkiem grupy użytkowników monitor wydajności lub mieć uprawnienia administracyjne.

Aby uniknąć konieczności podniesienia uprawnień dostępu do liczników wydajności w Windows Vista i nowszych, dodaj się do grupy użytkownicy monitor wydajności.

W systemie Windows Vista i nowszych Kontrola konta użytkownika (UAC) określa uprawnienia użytkownika. Jeśli jesteś członkiem wbudowanej grupy Administratorzy, masz przypisane dwa tokeny dostępu w czasie wykonywania: token dostępu użytkownika standardowego i token dostępu administratora. Domyślnie jesteś w roli użytkownika standardowego. Aby wykonać kod, który uzyskuje dostęp do liczników wydajności, należy najpierw podnieść poziom uprawnień od użytkownika standardowego do administratora. Możesz to zrobić po uruchomieniu aplikacji, klikając prawym przyciskiem myszy ikonę aplikacji i wskazując, że chcesz uruchomić jako administrator.

Zobacz też

Dotyczy