PerformanceCounterCategory.CounterExists Metodo

Definizione

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

Overload

CounterExists(String)

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

CounterExists(String, String)

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

CounterExists(String, String, String)

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

CounterExists(String)

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

Il nome del contatore delle prestazioni da ricercare.

Restituisce

Boolean

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

Eccezioni

L'elemento counterName è null.

La proprietà CategoryName non è stata impostata.

Una chiamata ad un'API di sistema sottostante non ha avuto esito positivo.

Codice eseguito senza privilegi di amministratore 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 CategoryName proprietà prima di chiamare questo metodo. In caso contrario, viene generata un'eccezione.

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

Nota

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 utenti di Monitor prestazioni 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 Monitor prestazioni.

In Windows Vista e versioni successive i privilegi di un utente sono determinati dalla funzionalità Controllo dell'account utente. Ai membri del gruppo Administrators predefinito vengono assegnati due token di accesso in fase di esecuzione, ovvero un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, viene assegnato il ruolo dell'utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è necessario innanzitutto elevare i privilegi dall'utente standard all'amministratore. È possibile farlo quando si avvia un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando l'opzione di esecuzione come amministratore.

Vedi anche

Si applica a

CounterExists(String, String)

Determina se il contatore specificato è registrato nella categoria specificata sul 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

Il nome del contatore delle prestazioni da ricercare.

categoryName
String

Il nome della categoria del contatore delle prestazioni o oggetto delle prestazioni cui è associato il contatore delle prestazioni specificato.

Restituisce

Boolean

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

Eccezioni

L'elemento categoryName è null.

-oppure- L'elemento counterName è null.

categoryName è una stringa vuota ("").

Il nome della categoria non esiste.

Una chiamata ad un'API di sistema sottostante non ha avuto esito positivo.

Codice eseguito senza privilegi di amministratore 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 contatore e un nome computer dalla riga di comando, se vengono 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

Nota

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 utenti Monitor prestazioni 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 Monitor prestazioni.

In Windows Vista e versioni successive i privilegi di un utente sono determinati dalla funzionalità Controllo dell'account utente. Ai membri del gruppo Administrators predefinito vengono assegnati due token di accesso in fase di esecuzione, ovvero un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, viene assegnato il ruolo dell'utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è prima necessario elevare i privilegi dall'utente standard all'amministratore. È possibile farlo quando si avvia un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando l'opzione di esecuzione come amministratore.

Vedi anche

Si applica a

CounterExists(String, String, String)

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

Il nome del contatore delle prestazioni da ricercare.

categoryName
String

Il nome della categoria del contatore delle prestazioni o oggetto delle prestazioni cui è associato il contatore delle prestazioni specificato.

machineName
String

Il nome del computer sul quale sono presenti la categoria del contatore delle prestazioni e i contatori associati.

Restituisce

Boolean

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

Eccezioni

L'elemento categoryName è null.

-oppure- L'elemento counterName è null.

categoryName è una stringa vuota ("").

-oppure- machineName non è valido.

Il nome della categoria non esiste.

Una chiamata ad un'API di sistema sottostante non ha avuto esito positivo.

Codice eseguito senza privilegi di amministratore 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 contatore e un nome computer dalla riga di comando, se vengono 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

Nota

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 utenti Monitor prestazioni 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 Monitor prestazioni.

In Windows Vista e versioni successive i privilegi di un utente sono determinati dalla funzionalità Controllo dell'account utente. Ai membri del gruppo Administrators predefinito vengono assegnati due token di accesso in fase di esecuzione, ovvero un token di accesso utente standard e un token di accesso amministratore. Per impostazione predefinita, viene assegnato il ruolo dell'utente standard. Per eseguire il codice che accede ai contatori delle prestazioni, è prima necessario elevare i privilegi dall'utente standard all'amministratore. È possibile farlo quando si avvia un'applicazione facendo clic con il pulsante destro del mouse sull'icona dell'applicazione e indicando l'opzione di esecuzione come amministratore.

Vedi anche

Si applica a