PerformanceCounterCategory.GetCategories 方法

定義

擷取登錄在電腦上的效能計數器分類清單。

多載

GetCategories()

擷取登錄在本機電腦上的效能計數器分類清單。

GetCategories(String)

擷取登錄在指定電腦上的效能計數器分類清單。

GetCategories()

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

擷取登錄在本機電腦上的效能計數器分類清單。

C#
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories ();

傳回

PerformanceCounterCategory 物件的陣列,指出登錄在本機電腦上的分類。

例外狀況

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例會 GetCategories 使用 方法,從本機計算機或指定的計算機傳回物件的陣列 PerformanceCounterCategory 。 它會將 PerformanceCounterCategory 陣列轉換成類別名稱的陣列,它會為使用者排序和顯示。 系統會 GetCategories 根據是否指定計算機名稱來選取多載。

C#
public static void Main(string[] args)
{
    string machineName = "";
    PerformanceCounterCategory[] categories;

    // Copy the machine name argument into the local variable.
    try
    {
        machineName = args[0]=="."? "": args[0];
    }
    catch
    {
    }

    // Generate a list of categories registered on the specified computer.
    try
    {
        if (machineName.Length > 0)
        {
            categories = PerformanceCounterCategory.GetCategories(machineName);
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get categories on " +
            (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    Console.WriteLine("These categories are registered on " +
        (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

    // Create and sort an array of category names.
    string[] categoryNames = new string[categories.Length];
    int objX;
    for(objX = 0; objX < categories.Length; objX++)
    {
        categoryNames[objX] = categories[objX].CategoryName;
    }
    Array.Sort(categoryNames);

    for(objX = 0; objX < categories.Length; objX++)
    {
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
    }
}

備註

備註

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

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

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

GetCategories(String)

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

擷取登錄在指定電腦上的效能計數器分類清單。

C#
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories (string machineName);

參數

machineName
String

要觀察的電腦。

傳回

PerformanceCounterCategory 物件的陣列,指出登錄在指定電腦上的分類。

例外狀況

machineName 參數無效。

對基礎系統 API 的呼叫失敗。

以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。

範例

下列程式代碼範例會 GetCategories 使用 方法,從本機計算機或指定的計算機傳回物件的陣列 PerformanceCounterCategory 。 它會將 PerformanceCounterCategory 陣列轉換成類別名稱的陣列,它會為使用者排序和顯示。 系統會 GetCategories 根據是否指定計算機名稱來選取多載。

C#
public static void Main(string[] args)
{
    string machineName = "";
    PerformanceCounterCategory[] categories;

    // Copy the machine name argument into the local variable.
    try
    {
        machineName = args[0]=="."? "": args[0];
    }
    catch
    {
    }

    // Generate a list of categories registered on the specified computer.
    try
    {
        if (machineName.Length > 0)
        {
            categories = PerformanceCounterCategory.GetCategories(machineName);
        }
        else
        {
            categories = PerformanceCounterCategory.GetCategories();
        }
    }
    catch(Exception ex)
    {
        Console.WriteLine("Unable to get categories on " +
            (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);
        Console.WriteLine(ex.Message);
        return;
    }

    Console.WriteLine("These categories are registered on " +
        (machineName.Length > 0 ? "computer \"{0}\":": "this computer:"), machineName);

    // Create and sort an array of category names.
    string[] categoryNames = new string[categories.Length];
    int objX;
    for(objX = 0; objX < categories.Length; objX++)
    {
        categoryNames[objX] = categories[objX].CategoryName;
    }
    Array.Sort(categoryNames);

    for(objX = 0; objX < categories.Length; objX++)
    {
        Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames[objX]);
    }
}

備註

若要擷取本機電腦上的類別,請使用另一個多載或傳遞 “.”。machineName

備註

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

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

在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。

另請參閱

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9