語言

PerformanceCounterCategory.Delete(String) 方法

定義

從本地電腦移除類別及其相關的計數器。

public:
 static void Delete(System::String ^ categoryName);
public static void Delete(string categoryName);
static member Delete : string -> unit
Public Shared Sub Delete (categoryName As String)

參數

categoryName
String

要刪除的自訂效能計數器類別名稱。

例外狀況

參數 categoryName 為 null。

該 categoryName 參數的語法無效。 它可能包含反斜線字元(“\”),或長度超過80個字元。

基礎系統 API 的呼叫失敗。

該分類無法刪除,因為它不是自訂類別。

未取得管理權限執行的程式碼嘗試讀取效能計數器。

範例

以下程式碼範例使用 Delete 刪除 a PerformanceCounterCategory 及其 PerformanceCounter 所包含物件的方法。

public static void Main(string[] args)
{
    string categoryName = "";

    // Copy the supplied argument into the local variable.
    try
    {
        categoryName = args[0];
    }
    catch (Exception ex)
    {
        Console.WriteLine("Missing argument identifying category to be deleted.");
    }

    // Delete the specified category.
    try
    {
        if (PerformanceCounterCategory.Exists(categoryName))
        {
            PerformanceCounterCategory.Delete(categoryName);
            Console.WriteLine("Category \"{0}\" deleted from this computer.", categoryName);
        }
        else
        {
            Console.WriteLine("Category name not found");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Unable to delete " +
            "category \"{0}\" from this computer:" + "\n" + ex.Message, categoryName);
    }
}
Sub Main(ByVal args() As String)
    Dim categoryName As String = ""

    ' Copy the supplied argument into the local variable.
    Try
        categoryName = args(0)
    Catch ex As Exception
        Console.WriteLine("Missing argument identifying category to be deleted.")
    End Try

    ' Delete the specified category.
    Try
        If (PerformanceCounterCategory.Exists(categoryName)) Then
            PerformanceCounterCategory.Delete(categoryName)
            Console.WriteLine( _
                "Category ""{0}"" deleted from this computer.", categoryName)
        Else
            Console.WriteLine("Category name not found")
        End If

    Catch ex As Exception
        Console.WriteLine("Unable to delete " & _
            "category ""{0}"" from this computer:" & vbCrLf & _
            ex.Message, categoryName)
    End Try
End Sub

備註

你只能從系統中刪除自訂的效能計數器類別。 你無法刪除分類中的計數器。 要做到這點,請刪除該類別,並用你想保留的計數器重新建立該類別。 為避免例外,刪除前請確認該分類是否存在。

Note

要讀取 Windows Vista 及以後版本、Windows XP Professional x64 版或 Windows Server 2003 中非互動式登入會話的效能計數器,您必須是效能監控使用者群組的成員或擁有管理員權限。

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

在 Windows Vista 和更新版本中,用戶帳戶控制 (UAC) 會決定用戶的許可權。 如果你是內建管理員群組的成員,你會被分配兩個執行時存取權杖:一個標準使用者存取權杖和一個管理員存取權杖。 根據預設,您處於標準使用者角色。 要執行存取效能計數器的程式碼,你必須先將權限從標準使用者提升為管理員。 當您以滑鼠右鍵按下應用程式圖示,並指出您想要以系統管理員身分執行,即可啟動應用程式。

適用於

另請參閱