共用方式為


PerformanceCounterCategory.InstanceExists 方法

定義

判斷類別是否包含指定的效能物件實例。

多載

InstanceExists(String)

判斷指定的性能物件實例是否存在於這個 PerformanceCounterCategory 物件的 CategoryName 屬性所識別的類別中。

InstanceExists(String, String)

判斷本機電腦上的指定類別是否包含指定的效能物件實例。

InstanceExists(String, String, String)

判斷指定計算機上的指定類別是否包含指定的效能物件實例。

InstanceExists(String)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

判斷指定的性能物件實例是否存在於這個 PerformanceCounterCategory 物件的 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

參數

instanceName
String

要搜尋的這個性能計數器類別中的性能物件實例。

傳回

如果類別包含指定的效能物件實例,true;否則,false

例外狀況

CategoryName 屬性 null。 屬性可能尚未設定。

instanceName 參數是 null

基礎系統 API 的呼叫失敗。

在沒有系統管理許可權的情況下執行的程式代碼嘗試讀取性能計數器。

範例

下列程式代碼範例會判斷 PerformanceCounter 實例是否存在於 PerformanceCounterCategory中。 它會先根據是否指定計算機名稱,使用適當的建構函式來建立 PerformanceCounterCategory 物件。 然後,它會使用 InstanceExists(String) 來判斷指定的實例是否存在,然後通知使用者。 如果未指定任何實例名稱,則此範例會使用預設的單一實例名稱。

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

備註

這個 InstanceExists 多載不是 static。 您必須建立 PerformanceCounterCategory 物件,並設定 CategoryName 屬性。

注意

若要從 Windows Vista 和更新版本中的非互動式登入會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是性能監視器使用者群組的成員或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至性能監視器使用者群組。

在 Windows Vista 和更新版本中,用戶帳戶控制 (UAC) 會決定用戶的許可權。 如果您是內建系統管理員群組的成員,系統會指派兩個運行時間存取令牌:標準使用者存取令牌和系統管理員存取令牌。 根據預設,您處於標準使用者角色。 若要執行可存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 當您以滑鼠右鍵按下應用程式圖示,並指出您想要以系統管理員身分執行,即可啟動應用程式。

另請參閱

適用於

InstanceExists(String, String)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

判斷本機電腦上的指定類別是否包含指定的效能物件實例。

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

參數

instanceName
String

要搜尋的效能物件實例。

categoryName
String

要搜尋的性能計數器類別目錄。

傳回

如果類別包含指定的效能物件實例,true;否則,false

例外狀況

instanceName 參數是 null

-或-

categoryName 參數是 null

categoryName 參數是空字串(“)。

基礎系統 API 的呼叫失敗。

在沒有系統管理許可權的情況下執行的程式代碼嘗試讀取性能計數器。

範例

下列程式代碼範例會使用 InstanceExists 的靜態多載來判斷指定的 PerformanceCounter 實例是否存在於 PerformanceCounterCategory中。 會根據是否指定計算機名稱來選取多載。 如果未指定任何實例名稱,則此範例會使用預設的單一實例名稱。

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

備註

無法判斷計算機上是否存在效能物件實例,而不需要指定要查看的特定類別。

注意

若要從 Windows Vista 和更新版本中的非互動式登入會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是性能監視器使用者群組的成員或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至性能監視器使用者群組。

在 Windows Vista 和更新版本中,用戶帳戶控制 (UAC) 會決定用戶的許可權。 如果您是內建系統管理員群組的成員,系統會指派兩個運行時間存取令牌:標準使用者存取令牌和系統管理員存取令牌。 根據預設,您處於標準使用者角色。 若要執行可存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 當您以滑鼠右鍵按下應用程式圖示,並指出您想要以系統管理員身分執行,即可啟動應用程式。

另請參閱

適用於

InstanceExists(String, String, String)

來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs
來源:
PerformanceCounterCategory.cs

判斷指定計算機上的指定類別是否包含指定的效能物件實例。

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

參數

instanceName
String

要搜尋的效能物件實例。

categoryName
String

要搜尋的性能計數器類別目錄。

machineName
String

要在其中尋找類別實例組的計算機名稱。

傳回

如果類別包含指定的效能物件實例,true;否則,false

例外狀況

instanceName 參數是 null

-或-

categoryName 參數是 null

categoryName 參數是空字串(“)。

-或-

machineName 參數無效。

基礎系統 API 的呼叫失敗。

在沒有系統管理許可權的情況下執行的程式代碼嘗試讀取性能計數器。

範例

下列程式代碼範例會使用 InstanceExists 的靜態多載來判斷指定的 PerformanceCounter 實例是否存在於 PerformanceCounterCategory中。 會根據是否指定計算機名稱來選取多載。 如果未指定任何實例名稱,則此範例會使用預設的單一實例名稱。

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

備註

無法判斷計算機上是否存在效能物件實例,而不需要指定要查看的特定類別。

您可以使用 「.」 來指定本機電腦。

注意

若要從 Windows Vista 和更新版本中的非互動式登入會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是性能監視器使用者群組的成員或具有系統管理許可權。

若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至性能監視器使用者群組。

在 Windows Vista 和更新版本中,用戶帳戶控制 (UAC) 會決定用戶的許可權。 如果您是內建系統管理員群組的成員,系統會指派兩個運行時間存取令牌:標準使用者存取令牌和系統管理員存取令牌。 根據預設,您處於標準使用者角色。 若要執行可存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 當您以滑鼠右鍵按下應用程式圖示,並指出您想要以系統管理員身分執行,即可啟動應用程式。

另請參閱

適用於