PerformanceCounterCategory.InstanceExists 方法

定义

确定该类别是否包含指定的性能对象实例。

重载

InstanceExists(String)

确定由此 PerformanceCounterCategory 对象的 CategoryName 属性标识的类别中是否存在指定的性能对象实例。

InstanceExists(String, String)

确定本地计算机上指定的类别是否包含指定的性能对象实例。

InstanceExists(String, String, String)

确定指定计算机上的指定类别是否包含指定的性能对象实例。

InstanceExists(String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
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) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于

InstanceExists(String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
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来确定 中PerformanceCounterCategory是否存在给定PerformanceCounter实例。 根据是否指定了计算机名称来选择重载。 如果未指定实例名称,则本示例使用默认的单实例名称。

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) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于

InstanceExists(String, String, String)

Source:
PerformanceCounterCategory.cs
Source:
PerformanceCounterCategory.cs
Source:
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来确定 中PerformanceCounterCategory是否存在给定PerformanceCounter实例。 根据是否指定了计算机名称来选择重载。 如果未指定实例名称,则本示例使用默认的单实例名称。

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) 决定用户的特权。 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。 默认情况下,您拥有标准用户角色。 若要执行访问性能计数器的代码,必须先将权限从标准用户提升为管理员。 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。

另请参阅

适用于