PerformanceCounterCategory.GetCategories 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取登錄在電腦上的效能計數器分類清單。
多載
GetCategories() |
擷取登錄在本機電腦上的效能計數器分類清單。 |
GetCategories(String) |
擷取登錄在指定電腦上的效能計數器分類清單。 |
GetCategories()
擷取登錄在本機電腦上的效能計數器分類清單。
public:
static cli::array <System::Diagnostics::PerformanceCounterCategory ^> ^ GetCategories();
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories ();
static member GetCategories : unit -> System.Diagnostics.PerformanceCounterCategory[]
Public Shared Function GetCategories () As PerformanceCounterCategory()
傳回
PerformanceCounterCategory 物件的陣列,指出登錄在本機電腦上的分類。
例外狀況
對基礎系統 API 的呼叫失敗。
以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。
範例
下列程式代碼範例會 GetCategories 使用 方法,從本機計算機或指定的計算機傳回物件的陣列 PerformanceCounterCategory 。 它會將 PerformanceCounterCategory 陣列轉換成類別名稱的陣列,它會為使用者排序和顯示。 系統會 GetCategories 根據是否指定計算機名稱來選取多載。
public:
static void Main(array<String^>^ args)
{
String^ machineName = "";
array<PerformanceCounterCategory^>^ categories;
// Copy the machine name argument into the local variable.
try
{
machineName = args[1]=="."? "": args[1];
}
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.
array<String^>^ categoryNames = gcnew array<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]);
}
}
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]);
}
}
Sub Main(ByVal args() As String)
Dim machineName As String = ""
Dim categories() As PerformanceCounterCategory
' Copy the machine name argument into the local variable.
Try
machineName = IIf(args(0) = ".", "", args(0))
Catch ex As Exception
End Try
' Generate a list of categories registered on the specified computer.
Try
If machineName.Length > 0 Then
categories = _
PerformanceCounterCategory.GetCategories(machineName)
Else
categories = PerformanceCounterCategory.GetCategories()
End If
Catch ex As Exception
Console.WriteLine("Unable to get categories on " & _
IIf(machineName.Length > 0, "computer ""{0}"":", _
"this computer:"), machineName)
Console.WriteLine(ex.Message)
Return
End Try
Console.WriteLine("These categories are registered on " & _
IIf(machineName.Length > 0, _
"computer ""{0}"":", "this computer:"), machineName)
' Create and sort an array of category names.
Dim categoryNames(categories.Length - 1) As String
Dim objX As Integer
For objX = 0 To categories.Length - 1
Console.WriteLine("{0}, {1}", objX, categories(objX).CategoryName)
categoryNames(objX) = categories(objX).CategoryName
Next objX
Array.Sort(categoryNames)
For objX = 0 To categories.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames(objX))
Next objX
End Sub
備註
注意
若要從 Windows Vista 和更新版本中的非互動式登入會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。
若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。
在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。
另請參閱
適用於
GetCategories(String)
擷取登錄在指定電腦上的效能計數器分類清單。
public:
static cli::array <System::Diagnostics::PerformanceCounterCategory ^> ^ GetCategories(System::String ^ machineName);
public static System.Diagnostics.PerformanceCounterCategory[] GetCategories (string machineName);
static member GetCategories : string -> System.Diagnostics.PerformanceCounterCategory[]
Public Shared Function GetCategories (machineName As String) As PerformanceCounterCategory()
參數
- machineName
- String
要觀察的電腦。
傳回
PerformanceCounterCategory 物件的陣列,指出登錄在指定電腦上的分類。
例外狀況
machineName
參數無效。
對基礎系統 API 的呼叫失敗。
以不具有系統管理員權限執行的程式碼嘗試讀取效能計數器。
範例
下列程式代碼範例會 GetCategories 使用 方法,從本機計算機或指定的計算機傳回物件的陣列 PerformanceCounterCategory 。 它會將 PerformanceCounterCategory 陣列轉換成類別名稱的陣列,它會為使用者排序和顯示。 系統會 GetCategories 根據是否指定計算機名稱來選取多載。
public:
static void Main(array<String^>^ args)
{
String^ machineName = "";
array<PerformanceCounterCategory^>^ categories;
// Copy the machine name argument into the local variable.
try
{
machineName = args[1]=="."? "": args[1];
}
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.
array<String^>^ categoryNames = gcnew array<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]);
}
}
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]);
}
}
Sub Main(ByVal args() As String)
Dim machineName As String = ""
Dim categories() As PerformanceCounterCategory
' Copy the machine name argument into the local variable.
Try
machineName = IIf(args(0) = ".", "", args(0))
Catch ex As Exception
End Try
' Generate a list of categories registered on the specified computer.
Try
If machineName.Length > 0 Then
categories = _
PerformanceCounterCategory.GetCategories(machineName)
Else
categories = PerformanceCounterCategory.GetCategories()
End If
Catch ex As Exception
Console.WriteLine("Unable to get categories on " & _
IIf(machineName.Length > 0, "computer ""{0}"":", _
"this computer:"), machineName)
Console.WriteLine(ex.Message)
Return
End Try
Console.WriteLine("These categories are registered on " & _
IIf(machineName.Length > 0, _
"computer ""{0}"":", "this computer:"), machineName)
' Create and sort an array of category names.
Dim categoryNames(categories.Length - 1) As String
Dim objX As Integer
For objX = 0 To categories.Length - 1
Console.WriteLine("{0}, {1}", objX, categories(objX).CategoryName)
categoryNames(objX) = categories(objX).CategoryName
Next objX
Array.Sort(categoryNames)
For objX = 0 To categories.Length - 1
Console.WriteLine("{0,4} - {1}", objX + 1, categoryNames(objX))
Next objX
End Sub
備註
若要擷取本機電腦上的類別,請使用另一個多載或傳遞 “.”。machineName
注意
若要從 Windows Vista 和更新版本中的非互動式登入會話讀取性能計數器、Windows XP Professional x64 Edition 或 Windows Server 2003,您必須是 效能監視器 Users 群組的成員,或具有系統管理許可權。
若要避免提高許可權以存取 Windows Vista 和更新版本中的性能計數器,請將您自己新增至 效能監視器 Users 群組。
在 Windows Vista (含) 以後版本中,使用者帳戶控制 (UAC) 會判斷使用者的權限。 如果您是內建 Administrators 群組的成員,系統會將兩個執行階段存取語彙基元 (Token) 指派給您:標準使用者存取語彙基元及管理員存取語彙基元。 根據預設,您會屬於標準使用者角色。 若要執行存取性能計數器的程式代碼,您必須先將許可權從標準使用者提升為系統管理員。 您可以在啟動應用程式時,以滑鼠右鍵按一下應用程式圖示,並指出您想要以系統管理員身分執行,藉此提高為系統管理員權限。