,而我等待更好的答案,這裏是一個完整的「rstpc.exe」實用程序來重置性能計數器(NumberOfItems32類型):
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace ResetPerformanceCounter
{
internal class Program
{
private static int Main(string[] args)
{
if (args.Length != 2)
{
string fileName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine("Usage: {0} <PC Category> <PC Name>", fileName);
Console.WriteLine("Examlpe: {0} {1} {2}", fileName, "GEF", "CommandCount");
return -1;
}
string cat = args[0];
string name = args[1];
if (!PerformanceCounterCategory.CounterExists(name, cat))
{
Console.WriteLine("Performance Counter {0}\\{1} not found.", cat, name);
return - 2;
}
var pc = new System.Diagnostics.PerformanceCounter(cat, name, false);
if (pc.CounterType != PerformanceCounterType.NumberOfItems32)
{
Console.WriteLine("Performance counter is of type {0}. Only '{1}' countres are supported.", pc.CounterType.ToString(), PerformanceCounterType.NumberOfItems32);
return -3;
}
Console.WriteLine("Old value: {0}", pc.RawValue);
pc.RawValue = 0;
Console.WriteLine("New value: {0}", pc.RawValue);
Console.WriteLine("Done.");
return 0;
}
}
}