Sdílet prostřednictvím


PerformanceCounterCategory.InstanceExists Metoda

Definice

Určuje, zda kategorie obsahuje zadanou instanci objektu výkonu.

Přetížení

InstanceExists(String)

Určuje, zda zadaná instance objektu výkonu existuje v kategorii, která je identifikována vlastností tohoto PerformanceCounterCategory objektu CategoryName .

InstanceExists(String, String)

Určuje, zda zadaná kategorie v místním počítači obsahuje zadanou instanci objektu výkonu.

InstanceExists(String, String, String)

Určuje, zda zadaná kategorie v zadaném počítači obsahuje zadanou instanci objektu výkonu.

InstanceExists(String)

Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs

Určuje, zda zadaná instance objektu výkonu existuje v kategorii, která je identifikována vlastností tohoto PerformanceCounterCategory objektu 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

Parametry

instanceName
String

Instance objektu výkonu v této kategorii čítače výkonu, která se má vyhledat.

Návraty

truepokud kategorie obsahuje zadanou instanci objektu výkonu; v opačném případě . false

Výjimky

Vlastnost CategoryName je null. Vlastnost možná nebyla nastavena.

Parametr instanceName je null.

Volání základního rozhraní API systému selhalo.

Kód, který se spouští bez oprávnění správce, se pokusil přečíst čítač výkonu.

Příklady

Následující příklad kódu určuje, jestli PerformanceCounter instance existuje v rámci objektu PerformanceCounterCategory. Nejprve vytvoří PerformanceCounterCategory objekt pomocí příslušného konstruktoru na základě toho, zda byl zadán název počítače. Pak použije InstanceExists(String) k určení, jestli zadaná instance existuje, a pak informuje uživatele. Pokud není zadán žádný název instance, použije se v příkladu výchozí název jedné instance.

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

Poznámky

Toto přetížení InstanceExists není static. Vyžaduje, abyste vytvořili PerformanceCounterCategory objekt a nastavili CategoryName vlastnost.

Poznámka

Chcete-li číst čítače výkonu z neinteraktivní přihlašovací relace v systému Windows Vista a novějších, Windows XP Professional x64 Edition nebo Windows Server 2003, musíte být členem skupiny Sledování výkonu Users nebo mít oprávnění správce.

Abyste nemuseli zvýšit oprávnění pro přístup k čítačům výkonu ve Windows Vista a novějších verzích, přidejte se do skupiny Sledování výkonu Users.

Nástroj Řízení uživatelských účtů (UAC) v systému Windows Vista a novějším určuje oprávnění uživatele. Pokud jste členem předdefinované skupiny Administrators, máte přiřazeny dva přístupové tokeny run-time: token přístupu uživatele se standardním oprávněním a token přístupu správce. Ve výchozím nastavení máte roli standardního uživatele. Pokud chcete spustit kód, který přistupuje k čítačům výkonu, musíte nejprve zvýšit oprávnění ze standardního uživatele na správce. Můžete to udělat při spuštění aplikace tak, že kliknete pravým tlačítkem na ikonu aplikace a označíte, že chcete spustit jako správce.

Viz také

Platí pro

InstanceExists(String, String)

Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs

Určuje, zda zadaná kategorie v místním počítači obsahuje zadanou instanci objektu výkonu.

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

Parametry

instanceName
String

Instance objektu výkonu, která se má vyhledat.

categoryName
String

Kategorie čítače výkonu, která se má hledat.

Návraty

truepokud kategorie obsahuje zadanou instanci objektu výkonu; v opačném případě . false

Výjimky

Parametr instanceName je null.

-nebo-

Parametr categoryName je null.

Parametr categoryName je prázdný řetězec ("").

Volání základního rozhraní API systému selhalo.

Kód, který se spouští bez oprávnění správce, se pokusil přečíst čítač výkonu.

Příklady

Následující příklad kódu používá statické přetížení k InstanceExists určení, zda daná PerformanceCounter instance existuje v objektu PerformanceCounterCategory. Přetížení je vybráno na základě toho, zda je zadán název počítače. Pokud není zadán žádný název instance, použije se v příkladu výchozí název jedné instance.

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

Poznámky

Není možné určit, zda instance objektu výkonu existuje v počítači bez určení konkrétní kategorie, ve které se má hledat.

Poznámka

Chcete-li číst čítače výkonu z neinteraktivní přihlašovací relace v systému Windows Vista a novějších, Windows XP Professional x64 Edition nebo Windows Server 2003, musíte být členem skupiny Sledování výkonu Users nebo mít oprávnění správce.

Abyste nemuseli zvýšit oprávnění pro přístup k čítačům výkonu ve Windows Vista a novějších verzích, přidejte se do skupiny Sledování výkonu Users.

Nástroj Řízení uživatelských účtů (UAC) v systému Windows Vista a novějším určuje oprávnění uživatele. Pokud jste členem předdefinované skupiny Administrators, máte přiřazeny dva přístupové tokeny run-time: token přístupu uživatele se standardním oprávněním a token přístupu správce. Ve výchozím nastavení máte roli standardního uživatele. Pokud chcete spustit kód, který přistupuje k čítačům výkonu, musíte nejprve zvýšit oprávnění ze standardního uživatele na správce. Můžete to udělat při spuštění aplikace tak, že kliknete pravým tlačítkem na ikonu aplikace a označíte, že chcete spustit jako správce.

Viz také

Platí pro

InstanceExists(String, String, String)

Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs
Zdroj:
PerformanceCounterCategory.cs

Určuje, zda zadaná kategorie v zadaném počítači obsahuje zadanou instanci objektu výkonu.

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

Parametry

instanceName
String

Instance objektu výkonu, která se má vyhledat.

categoryName
String

Kategorie čítače výkonu, která se má hledat.

machineName
String

Název počítače, na kterém se má vyhledat dvojice instancí kategorie.

Návraty

truepokud kategorie obsahuje zadanou instanci objektu výkonu; v opačném případě . false

Výjimky

Parametr instanceName je null.

-nebo-

Parametr categoryName je null.

Parametr categoryName je prázdný řetězec ("").

-nebo-

Parametr machineName je neplatný.

Volání základního rozhraní API systému selhalo.

Kód, který se spouští bez oprávnění správce, se pokusil přečíst čítač výkonu.

Příklady

Následující příklad kódu používá statické přetížení k InstanceExists určení, zda daná PerformanceCounter instance existuje v objektu PerformanceCounterCategory. Přetížení je vybráno na základě toho, zda je zadán název počítače. Pokud není zadán žádný název instance, použije se v příkladu výchozí název jedné instance.

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

Poznámky

Není možné určit, zda instance objektu výkonu existuje v počítači bez určení konkrétní kategorie, ve které se má hledat.

K určení místního počítače můžete použít "." .

Poznámka

Chcete-li číst čítače výkonu z neinteraktivní přihlašovací relace v systému Windows Vista a novějších, Windows XP Professional x64 Edition nebo Windows Server 2003, musíte být členem skupiny Sledování výkonu Users nebo mít oprávnění správce.

Abyste nemuseli zvýšit oprávnění pro přístup k čítačům výkonu ve Windows Vista a novějších verzích, přidejte se do skupiny Sledování výkonu Users.

Nástroj Řízení uživatelských účtů (UAC) v systému Windows Vista a novějším určuje oprávnění uživatele. Pokud jste členem předdefinované skupiny Administrators, máte přiřazeny dva přístupové tokeny run-time: token přístupu uživatele se standardním oprávněním a token přístupu správce. Ve výchozím nastavení máte roli standardního uživatele. Pokud chcete spustit kód, který přistupuje k čítačům výkonu, musíte nejprve zvýšit oprávnění ze standardního uživatele na správce. Můžete to udělat při spuštění aplikace tak, že kliknete pravým tlačítkem na ikonu aplikace a označíte, že chcete spustit jako správce.

Viz také

Platí pro