PerformanceCounterCategory.GetInstanceNames 方法

定義

擷取與這個分類相關的效能物件執行個體清單。

C#
public string[] GetInstanceNames ();

傳回

String[]

字串的陣列,表示與這個分類相關的效能物件執行個體名稱,或者包含空字串 ("") 的單一項目陣列 (如果分類只包含一個效能物件執行個體)。

例外狀況

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

-或-

此分類沒有相關聯的執行個體。

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

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

範例

下列程式代碼範例會取得 中的PerformanceCounterCategory物件清單PerformanceCounter。 它會先根據是否指定計算機名稱,使用適當的建構函式來建立 PerformanceCounterCategory 物件。 然後,它會使用 GetInstanceNames 傳回實例名稱做為 的 String陣列,它會排序和顯示。

C#
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]);
        }
    }
}

備註

備註

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

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

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

適用於

產品 版本
.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

另請參閱