PerformanceCounterCategory.GetInstanceNames 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取與此類別相關聯的效能物件實例清單。
public:
cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()
傳回
一組字串陣列代表與此類別相關的效能物件實例名稱;若類別僅包含一個效能物件實例,則為單項陣列,包含空字串(“”)。
例外狀況
基礎系統 API 的呼叫失敗。
未取得管理權限執行的程式碼嘗試讀取效能計數器。
範例
以下程式碼範例會得到一個物件清單PerformanceCounter。PerformanceCounterCategory 它會先根據是否指定計算機名稱,使用適當的建構函式來建立 PerformanceCounterCategory 物件。 接著它會將 GetInstanceNames 實例名稱以陣列 String的形式回傳,並進行排序與顯示。
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
備註
Note
要讀取 Windows Vista 及以後版本、Windows XP Professional x64 版或 Windows Server 2003 中非互動式登入會話的效能計數器,您必須是效能監控使用者群組的成員或擁有管理員權限。
若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至性能監視器使用者群組。
在 Windows Vista 和更新版本中,用戶帳戶控制 (UAC) 會決定用戶的許可權。 如果你是內建管理員群組的成員,你會被分配兩個執行時存取權杖:一個標準使用者存取權杖和一個管理員存取權杖。 根據預設,您處於標準使用者角色。 要執行存取效能計數器的程式碼,你必須先將權限從標準使用者提升為管理員。 當您以滑鼠右鍵按下應用程式圖示,並指出您想要以系統管理員身分執行,即可啟動應用程式。