PerformanceCounterCategory.InstanceExists Metodo

Definizione

Determina se la categoria contiene l'istanza dell'oggetto delle prestazioni specificato.

Overload

InstanceExists(String)

Determina se l'istanza dell'oggetto delle prestazioni specificato è presente nella categoria identificata dalla proprietà PerformanceCounterCategory di questo oggetto CategoryName.

InstanceExists(String, String)

Determina se una categoria specificata sul computer locale contiene l'istanza dell'oggetto delle prestazioni specificato.

InstanceExists(String, String, String)

Determina se una categoria specificata su un particolare computer contiene l'istanza dell'oggetto delle prestazioni specificato.

InstanceExists(String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determina se l'istanza dell'oggetto delle prestazioni specificato è presente nella categoria identificata dalla proprietà PerformanceCounterCategory di questo oggetto CategoryName.

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

Parametri

instanceName
String

L'istanza dell'oggetto delle prestazioni da ricercare in questa categoria di contatori delle prestazioni.

Restituisce

true se la categoria contiene l'istanza dell'oggetto delle prestazioni specificato; in caso contrario, false .

Eccezioni

La proprietà CategoryName è null. È probabile che la proprietà non sia stata impostata.

Il valore del parametro instanceName è null.

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'istanza PerformanceCounter all'interno di un oggetto PerformanceCounterCategory. Crea innanzitutto un PerformanceCounterCategory oggetto utilizzando il costruttore appropriato in base al fatto che sia stato specificato un nome computer. Viene quindi usato InstanceExists(String) per determinare se l'istanza specificata esiste, quindi informa l'utente. Se non viene specificato alcun nome di istanza, nell'esempio viene utilizzato il nome predefinito a istanza singola.

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

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

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

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

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

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

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

        ' Check whether the instance exists.
        ' Use the per-instance overload of InstanceExists.
        objectExists = pcc.InstanceExists(instanceName)

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

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

Commenti

Questo overload di InstanceExists non staticè . È necessario creare un PerformanceCounterCategory oggetto e impostare la CategoryName proprietà .

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

InstanceExists(String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determina se una categoria specificata sul computer locale contiene l'istanza dell'oggetto delle prestazioni specificato.

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

Parametri

instanceName
String

L'istanza dell'oggetto delle prestazioni da ricercare.

categoryName
String

La categoria di contatori delle prestazioni da cercare.

Restituisce

true se la categoria contiene l'istanza dell'oggetto delle prestazioni specificato; in caso contrario, false .

Eccezioni

Il valore del parametro instanceName è null.

-oppure-

Il valore del parametro categoryName è null.

Il parametro categoryName è una stringa vuota ("").

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 vengono utilizzati gli overload statici di InstanceExists per determinare se l'istanza specificata PerformanceCounter esiste in PerformanceCounterCategory. L'overload viene selezionato in base al fatto che sia specificato un nome computer. Se non viene specificato alcun nome di istanza, nell'esempio viene utilizzato il nome predefinito a istanza singola.

public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

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

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

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

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

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

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

Commenti

Non è possibile determinare se un'istanza dell'oggetto prestazioni esiste in un computer senza specificare una categoria specifica da cercare.

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

InstanceExists(String, String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs

Determina se una categoria specificata su un particolare computer contiene l'istanza dell'oggetto delle prestazioni specificato.

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

Parametri

instanceName
String

L'istanza dell'oggetto delle prestazioni da ricercare.

categoryName
String

La categoria di contatori delle prestazioni da cercare.

machineName
String

Il nome del computer sul quale ricercare la coppia di istanze della categoria.

Restituisce

true se la categoria contiene l'istanza dell'oggetto delle prestazioni specificato; in caso contrario, false .

Eccezioni

Il valore del parametro instanceName è null.

-oppure-

Il valore del parametro categoryName è null.

Il parametro categoryName è una stringa vuota ("").

-oppure-

Il parametro machineName non è valido.

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 vengono utilizzati gli overload statici di InstanceExists per determinare se l'istanza specificata PerformanceCounter esiste in PerformanceCounterCategory. L'overload viene selezionato in base al fatto che sia specificato un nome computer. Se non viene specificato alcun nome di istanza, nell'esempio viene utilizzato il nome predefinito a istanza singola.

public static void Main(string[] args)
{
    string categoryName = "";
    string instanceName = "";
    string machineName = "";
    bool objectExists = false;
    const string SINGLE_INSTANCE_NAME = "systemdiagnosticsperfcounterlibsingleinstance";

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

    // Use the given instance name or use the default single-instance name.
    if (instanceName.Length==0)
    {
        instanceName = SINGLE_INSTANCE_NAME;
    }

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

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

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

    ' Use the given instance name or use the default single-instance name.
    If instanceName.Length = 0 Then
        instanceName = SINGLE_INSTANCE_NAME
    End If

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

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

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

Commenti

Non è possibile determinare se un'istanza dell'oggetto prestazioni esiste in un computer senza specificare una categoria specifica da cercare.

È possibile usare "." per specificare 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