PerformanceCounterCategory.GetInstanceNames メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
このカテゴリに関連付けられているパフォーマンス オブジェクト インスタンスの一覧を取得します。
public:
cli::array <System::String ^> ^ GetInstanceNames();
public string[] GetInstanceNames();
member this.GetInstanceNames : unit -> string[]
Public Function GetInstanceNames () As String()
返品
このカテゴリに関連付けられているパフォーマンス オブジェクト インスタンス名を表す文字列の配列。カテゴリにパフォーマンス オブジェクト インスタンスが 1 つだけ含まれている場合は、空の文字列 ("") を含む単一エントリ配列。
例外
基になるシステム API の呼び出しに失敗しました。
管理特権なしで実行されているコードが、パフォーマンス カウンターを読み取ろうとしました。
例
次のコード例では、PerformanceCounterCategory内のPerformanceCounter オブジェクトの一覧を取得します。 最初に、コンピューター名が指定されたかどうかに基づいて適切なコンストラクターを使用して、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 Edition、または Windows Server 2003 の非対話型ログオン セッションからパフォーマンス カウンターを読み取る場合は、パフォーマンス モニター ユーザー グループのメンバーであるか、管理者特権を持っている必要があります。
Windows Vista 以降のパフォーマンス カウンターにアクセスするために特権を昇格する必要を回避するには、パフォーマンス モニター ユーザー グループに自分自身を追加します。
Windows Vista 以降では、ユーザー アカウント制御 (UAC) によってユーザーの権限が決定されます。 組み込みの Administrators グループのメンバーである場合は、標準ユーザー アクセス トークンと管理者アクセス トークンという 2 つのランタイム アクセス トークンが割り当てられます。 既定では、標準のユーザー ロールになります。 パフォーマンス カウンターにアクセスするコードを実行するには、まず特権を標準ユーザーから管理者に昇格させる必要があります。 これを行うには、アプリケーション アイコンを右クリックし、管理者として実行することを示すことで、アプリケーションを起動します。