Condividi tramite


PerformanceCounterCategory.CounterExists Metodo

Definizione

Determina se un contatore specificato è registrato in una determinata categoria.

Overload

Nome Descrizione
CounterExists(String)

Determina se il contatore specificato è registrato in questa categoria, indicato dalle CategoryName proprietà e MachineName .

CounterExists(String, String)

Determina se il contatore specificato è registrato nella categoria specificata nel computer locale.

CounterExists(String, String, String)

Determina se il contatore specificato è registrato nella categoria specificata in un computer remoto.

CounterExists(String)

Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs

Determina se il contatore specificato è registrato in questa categoria, indicato dalle CategoryName proprietà e 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

Parametri

counterName
String

Nome del contatore delle prestazioni da cercare.

Restituisce

true se il contatore è registrato nella categoria specificata dalle CategoryName proprietà e MachineName ; in caso contrario, false.

Eccezioni

Il counterName è null.

La CategoryName proprietà non è stata impostata.

Chiamata a un'API di sistema sottostante non riuscita.

Codice in esecuzione senza privilegi amministrativi che ha tentato di leggere un contatore delle prestazioni.

Esempio

Nell'esempio di codice seguente viene determinato se esiste un oggetto PerformanceCounter . Ottiene un nome di categoria, un nome del contatore e un nome computer dalla riga di comando, se specificati. Crea un PerformanceCounterCategory oggetto usando l'oggetto appropriato PerformanceCounterCategory. Usa quindi il CounterExists(String) metodo per determinare se l'oggetto specificato PerformanceCounter esiste e informa l'utente.

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

Commenti

È necessario impostare la proprietà prima di CategoryName chiamare questo metodo. In caso contrario, viene generata un'eccezione.

Se la MachineName proprietà non è stata impostata, questo metodo utilizza il computer locale (".").

Annotazioni

Per leggere i contatori delle prestazioni da una sessione di accesso non interattiva in Windows Vista e versioni successive, Windows XP Professional x64 Edition o Windows Server 2003, è necessario essere membri del gruppo Performance Monitor Users o disporre di privilegi amministrativi.

Per evitare di dover elevare i privilegi per accedere ai contatori delle prestazioni in Windows Vista e versioni successive, aggiungere se stessi al gruppo Utenti di Performance Monitor.

In Windows Vista e versioni successive, Controllo account utente determina i privilegi di un utente. Se si è membri del gruppo Administrators predefinito, vengono assegnati due token di accesso in fase di esecuzione: un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, si è nel ruolo utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è necessario innanzitutto elevare i privilegi dall'utente standard all'amministratore. A tale scopo, è possibile avviare un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando che si vuole eseguire come amministratore.

Vedi anche

Si applica a

CounterExists(String, String)

Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs

Determina se il contatore specificato è registrato nella categoria specificata nel computer locale.

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

Parametri

counterName
String

Nome del contatore delle prestazioni da cercare.

categoryName
String

Nome della categoria del contatore delle prestazioni o dell'oggetto prestazioni a cui è associato il contatore delle prestazioni specificato.

Restituisce

true, se il contatore è registrato nella categoria specificata nel computer locale; in caso contrario, false.

Eccezioni

Il categoryName è null.

oppure

Il counterName è null.

categoryName è una stringa vuota ("").

Il nome della categoria non esiste.

Chiamata a un'API di sistema sottostante non riuscita.

Codice in esecuzione senza privilegi amministrativi che ha tentato di leggere un contatore delle prestazioni.

Esempio

Nell'esempio di codice seguente viene determinato se esiste un oggetto PerformanceCounter . Ottiene un nome di categoria, un nome del contatore e un nome computer dalla riga di comando, se specificati. Usa gli overload statici del CounterExists metodo per determinare se il nome specificato PerformanceCounter esiste nell'oggetto PerformanceCounterCategory. L'overload viene selezionato in base al fatto che venga specificato un nome computer.

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

Commenti

Annotazioni

Per leggere i contatori delle prestazioni da una sessione di accesso non interattiva in Windows Vista e versioni successive, Windows XP Professional x64 Edition o Windows Server 2003, è necessario essere membri del gruppo Performance Monitor Users o disporre di privilegi amministrativi.

Per evitare di dover elevare i privilegi per accedere ai contatori delle prestazioni in Windows Vista e versioni successive, aggiungere se stessi al gruppo Utenti di Performance Monitor.

In Windows Vista e versioni successive, Controllo account utente determina i privilegi di un utente. Se si è membri del gruppo Administrators predefinito, vengono assegnati due token di accesso in fase di esecuzione: un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, si è nel ruolo utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è necessario innanzitutto elevare i privilegi dall'utente standard all'amministratore. A tale scopo, è possibile avviare un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando che si vuole eseguire come amministratore.

Vedi anche

Si applica a

CounterExists(String, String, String)

Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs
Origine:
PerformanceCounterCategory.cs

Determina se il contatore specificato è registrato nella categoria specificata in un computer remoto.

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

Parametri

counterName
String

Nome del contatore delle prestazioni da cercare.

categoryName
String

Nome della categoria del contatore delle prestazioni o dell'oggetto prestazioni a cui è associato il contatore delle prestazioni specificato.

machineName
String

Nome del computer in cui sono presenti la categoria del contatore delle prestazioni e i contatori associati.

Restituisce

true, se il contatore è registrato nella categoria specificata nel computer specificato; in caso contrario, false.

Eccezioni

Il categoryName è null.

oppure

Il counterName è null.

categoryName è una stringa vuota ("").

oppure

L'oggetto machineName non è valido.

Il nome della categoria non esiste.

Chiamata a un'API di sistema sottostante non riuscita.

Codice in esecuzione senza privilegi amministrativi che ha tentato di leggere un contatore delle prestazioni.

Esempio

Nell'esempio di codice seguente viene determinato se esiste un oggetto PerformanceCounter . Ottiene un nome di categoria, un nome del contatore e un nome computer dalla riga di comando, se specificati. Usa gli overload statici del CounterExists metodo per determinare se il nome specificato PerformanceCounter esiste nell'oggetto PerformanceCounterCategory. L'overload viene selezionato in base al fatto che venga specificato un nome computer.

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

Commenti

Annotazioni

Per leggere i contatori delle prestazioni da una sessione di accesso non interattiva in Windows Vista e versioni successive, Windows XP Professional x64 Edition o Windows Server 2003, è necessario essere membri del gruppo Performance Monitor Users o disporre di privilegi amministrativi.

Per evitare di dover elevare i privilegi per accedere ai contatori delle prestazioni in Windows Vista e versioni successive, aggiungere se stessi al gruppo Utenti di Performance Monitor.

In Windows Vista e versioni successive, Controllo account utente determina i privilegi di un utente. Se si è membri del gruppo Administrators predefinito, vengono assegnati due token di accesso in fase di esecuzione: un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, si è nel ruolo utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è necessario innanzitutto elevare i privilegi dall'utente standard all'amministratore. A tale scopo, è possibile avviare un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando che si vuole eseguire come amministratore.

Vedi anche

Si applica a