PerformanceCounterCategory.GetInstanceNames 메서드

정의

이 범주와 관련된 성능 개체 인스턴스 목록을 검색합니다.

public:
 cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames ();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()

반환

String[]

이 범주와 관련된 성능 개체 인스턴스 이름을 나타내는 문자열 배열이거나, 해당 범주에 성능 개체 인스턴스가 하나만 있는 경우 빈 문자열("")을 포함하는 단일 엔트리 배열입니다.

예외

CategoryName 속성은 null입니다. 속성을 설정하지 않았을 수도 있습니다.

또는

범주에 연관된 인스턴스가 없는 경우

내부 시스템 API를 호출하지 못한 경우

관리자 권한 없이 실행되는 코드가 성능 카운터를 읽으려고 한 경우

예제

다음 코드 예제에서는 의 개체 PerformanceCounterCategory목록을 PerformanceCounter 가져옵니다. 먼저 컴퓨터 이름이 지정되었는지 여부에 따라 적절한 생성자를 사용하여 개체를 만듭니다 PerformanceCounterCategory . 그런 다음 를 사용하여 GetInstanceNames instance 이름을 정렬하고 표시하는 의 String배열로 반환합니다.

public:
    static void Main(array<String^>^ args)
    {
        String^ categoryName = "";
        String^ machineName = "";
        PerformanceCounterCategory^ pcc;
        array<String^>^ instances;

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

        try
        {
            // Create the appropriate PerformanceCounterCategory object.
            if (machineName->Length > 0)
            {
                pcc = gcnew PerformanceCounterCategory(categoryName, machineName);
            }
            else
            {
                pcc = gcnew PerformanceCounterCategory(categoryName);
            }

            // Get the instances associated with this category.
            instances = pcc->GetInstanceNames();

        }
        catch (Exception^ ex)
        {
            Console::WriteLine("Unable to get instance information for " +
                "category \"{0}\" on " + 
                (machineName->Length>0? "computer \"{1}\":": "this computer:"),
                categoryName, machineName);
            Console::WriteLine(ex->Message);
            return;
        }

        //If an empty array is returned, the category has a single instance.
        if (instances->Length==0)
        {
            Console::WriteLine("Category \"{0}\" on " +
                (machineName->Length>0? "computer \"{1}\"": "this computer") +
                " is single-instance.", pcc->CategoryName, pcc->MachineName);
        }
        else
        {
            // Otherwise, display the instances.
            Console::WriteLine("These instances exist in category \"{0}\" on " +
                (machineName->Length>0? "computer \"{1}\".": "this computer:"),
                pcc->CategoryName, pcc->MachineName);

            Array::Sort(instances);
            int objX;
            for (objX = 0; objX < instances->Length; objX++)
            {
                Console::WriteLine("{0,4} - {1}", objX+1, instances[objX]);
            }
        }
    }
public static void Main(string[] args)
{
    string categoryName = "";
    string machineName = "";
    PerformanceCounterCategory pcc;
    string[] instances;

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

    try
    {
        // Create the appropriate PerformanceCounterCategory object.
        if (machineName.Length>0)
        {
            pcc = new PerformanceCounterCategory(categoryName, machineName);
        }
        else
        {
            pcc = new PerformanceCounterCategory(categoryName);
        }

        // Get the instances associated with this category.
        instances = pcc.GetInstanceNames();
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get instance information for " +
            "category \"{0}\" on " +
            (machineName.Length>0? "computer \"{1}\":": "this computer:"),
            categoryName, machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    //If an empty array is returned, the category has a single instance.
    if (instances.Length==0)
    {
        Console.WriteLine("Category \"{0}\" on " +
            (machineName.Length>0? "computer \"{1}\"": "this computer") +
            " is single-instance.", pcc.CategoryName, pcc.MachineName);
    }
    else
    {
        // Otherwise, display the instances.
        Console.WriteLine("These instances exist in category \"{0}\" on " +
            (machineName.Length>0? "computer \"{1}\".": "this computer:"),
            pcc.CategoryName, pcc.MachineName);

        Array.Sort(instances);
        int objX;
        for(objX=0; objX<instances.Length; objX++)
        {
            Console.WriteLine("{0,4} - {1}", objX+1, instances[objX]);
        }
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""
    Dim machineName As String = ""
    Dim pcc As PerformanceCounterCategory
    Dim instances() As String

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

    Try
        ' Create the appropriate PerformanceCounterCategory object.
        If machineName.Length > 0 Then
            pcc = New PerformanceCounterCategory(categoryName, machineName)
        Else
            pcc = New PerformanceCounterCategory(categoryName)
        End If

        ' Get the instances associated with this category.
        instances = pcc.GetInstanceNames()

    Catch ex As Exception
        Console.WriteLine("Unable to get instance information for " & _
             "category ""{0}"" on " & IIf(machineName.Length > 0, _
             "computer ""{1}"":", "this computer:"), _
             categoryName, machineName)
        Console.WriteLine(ex.Message)
        Return
    End Try

    'If an empty array is returned, the category has a single instance.
    If instances.Length = 0 Then
        Console.WriteLine( _
            "Category ""{0}"" on " & IIf(machineName.Length > 0, _
            "computer ""{1}""", "this computer") & _
            " is single-instance.", pcc.CategoryName, pcc.MachineName)
    Else
        ' Otherwise, display the instances.
        Console.WriteLine( _
            "These instances exist in category ""{0}"" on " & _
            IIf(machineName.Length > 0, _
                "computer ""{1}"".", "this computer:"), _
            pcc.CategoryName, pcc.MachineName)

        Array.Sort(instances)
        Dim objX As Integer
        For objX = 0 To instances.Length - 1
            Console.WriteLine("{0,4} - {1}", objX + 1, instances(objX))
        Next objX
    End If
End Sub

설명

참고

Windows Vista 이상, Windows XP Professional x64 Edition 또는 Windows Server 2003의 비대화형 로그온 세션에서 성능 카운터를 읽으려면 성능 모니터 사용자 그룹의 구성원이거나 관리 권한이 있어야 합니다.

Windows Vista 이상에서 성능 카운터에 액세스하기 위해 권한을 상승시킬 필요가 없도록 하려면 성능 모니터 사용자 그룹에 자신을 추가합니다.

Windows Vista 이상에서는 UAC(사용자 계정 컨트롤)가 사용자 권한을 결정합니다. 기본 제공 Administrators 그룹의 멤버인 경우 두 개의 런타임 액세스 토큰(표준 사용자 액세스 토큰 및 관리자 액세스 토큰)이 할당됩니다. 기본적으로 표준 사용자 역할이 지정됩니다. 성능 카운터에 액세스하는 코드를 실행하려면 먼저 표준 사용자에서 관리자로 권한을 상승시켜야 합니다. 애플리케이션 아이콘을 마우스 오른쪽 단추로 클릭하고 관리자로 실행하도록 지정하여 애플리케이션을 시작하면 이 작업을 수행할 수 있습니다.

적용 대상

추가 정보