PerformanceCounterCategory.InstanceExists Metoda

Definicja

Określa, czy kategoria zawiera określone wystąpienie obiektu wydajności.

Przeciążenia

InstanceExists(String)

Określa, czy określone wystąpienie obiektu wydajności istnieje w kategorii, która jest identyfikowana przez właściwość tego PerformanceCounterCategory obiektu CategoryName .

InstanceExists(String, String)

Określa, czy określona kategoria na komputerze lokalnym zawiera określone wystąpienie obiektu wydajności.

InstanceExists(String, String, String)

Określa, czy określona kategoria na określonym komputerze zawiera określone wystąpienie obiektu wydajności.

InstanceExists(String)

Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs

Określa, czy określone wystąpienie obiektu wydajności istnieje w kategorii, która jest identyfikowana przez właściwość tego PerformanceCounterCategory obiektu 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

Wystąpienie obiektu wydajności w tej kategorii licznika wydajności do wyszukania.

Zwraca

true jeśli kategoria zawiera określone wystąpienie obiektu wydajności; w przeciwnym razie , false.

Wyjątki

Właściwość CategoryName to null. Właściwość mogła nie zostać ustawiona.

Parametr instanceName ma wartość null.

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 PerformanceCounter wystąpienie istnieje w obiekcie PerformanceCounterCategory. Najpierw tworzy PerformanceCounterCategory obiekt przy użyciu odpowiedniego konstruktora na podstawie tego, czy określono nazwę komputera. Następnie służy InstanceExists(String) do określania, czy określone wystąpienie istnieje, a następnie informuje użytkownika. Jeśli nazwa wystąpienia nie zostanie określona, w przykładzie użyto domyślnej nazwy pojedynczego wystąpienia.

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

Uwagi

To przeciążenie InstanceExists elementu nie staticma wartości . Wymaga to utworzenia PerformanceCounterCategory obiektu i ustawienia CategoryName właściwości .

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w systemie 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 systemie Windows Vista i nowszych, dodaj siebie 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

InstanceExists(String, String)

Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs

Określa, czy określona kategoria na komputerze lokalnym zawiera określone wystąpienie obiektu wydajności.

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

Wystąpienie obiektu wydajności do wyszukania.

categoryName
String

Kategoria licznika wydajności do wyszukania.

Zwraca

true jeśli kategoria zawiera określone wystąpienie obiektu wydajności; w przeciwnym razie , false.

Wyjątki

Parametr instanceName ma wartość null.

-lub-

Parametr categoryName ma wartość null.

Parametr categoryName jest pustym ciągiem ("").

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

W poniższym przykładzie kodu użyto statycznych przeciążeń elementu InstanceExists , aby określić, czy dane PerformanceCounter wystąpienie istnieje w obiekcie PerformanceCounterCategory. Przeciążenie jest wybierane na podstawie tego, czy określono nazwę komputera. Jeśli nazwa wystąpienia nie zostanie określona, w przykładzie użyto domyślnej nazwy pojedynczego wystąpienia.

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

Uwagi

Nie można określić, czy wystąpienie obiektu wydajności istnieje na komputerze bez określania określonej kategorii do zaglądania.

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w systemie 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 systemie Windows Vista i nowszych, dodaj siebie 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

InstanceExists(String, String, String)

Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs
Źródło:
PerformanceCounterCategory.cs

Określa, czy określona kategoria na określonym komputerze zawiera określone wystąpienie obiektu wydajności.

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

Wystąpienie obiektu wydajności do wyszukania.

categoryName
String

Kategoria licznika wydajności do wyszukania.

machineName
String

Nazwa komputera, na którym należy szukać pary wystąpień kategorii.

Zwraca

true jeśli kategoria zawiera określone wystąpienie obiektu wydajności; w przeciwnym razie , false.

Wyjątki

Parametr instanceName ma wartość null.

-lub-

Parametr categoryName ma wartość null.

Parametr categoryName jest pustym ciągiem ("").

-lub-

Parametr machineName jest nieprawidłowy.

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

W poniższym przykładzie kodu użyto statycznych przeciążeń elementu InstanceExists , aby określić, czy dane PerformanceCounter wystąpienie istnieje w obiekcie PerformanceCounterCategory. Przeciążenie jest wybierane na podstawie tego, czy określono nazwę komputera. Jeśli nazwa wystąpienia nie zostanie określona, w przykładzie użyto domyślnej nazwy pojedynczego wystąpienia.

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

Uwagi

Nie można określić, czy wystąpienie obiektu wydajności istnieje na komputerze bez określania określonej kategorii do zaglądania.

Można użyć ." do określenia komputera lokalnego.

Uwaga

Aby odczytać liczniki wydajności z sesji logowania nieinterakcyjnego w systemie 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 systemie Windows Vista i nowszych, dodaj siebie 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