如何在不删除旧计数器的情况下将新计数器添加到现有性能计数器类别?

我有一个自定义计数器类别,我需要添加一个新的计数器,而不删除或重置任何现有的计数器。 我怎样才能做到这一点?

我尝试使用CounterExists(),但即使在我创建计数器之后,如何将它与CounterCreationDataCollection项关联并将其与我现有的计数器类别相关联?

我发现这样做的最好方法,特别是因为这个主题似乎没有太多信息,是保留现有的原始值,然后在删除并重新创建类别后重新应用它们。

///  /// When deleting the Category, need to preserve the existing counter values ///  static Dictionary GetPreservedValues(string category, XmlNodeList nodes) { Dictionary preservedValues = new Dictionary(); foreach (XmlNode counterNode in nodes) { string counterName = counterNode.Attributes["name"].Value; if (PerformanceCounterCategory.CounterExists(counterName, category)) { PerformanceCounter performanceCounter = new PerformanceCounter(category, counterName, false); preservedValues.Add(counterName, performanceCounter.RawValue); Console.WriteLine("Preserving {0} with a RawValue of {1}", counterName, performanceCounter.RawValue); } else { Console.WriteLine("Unable to preserve {0} because it doesn't exist", counterName); } } return preservedValues; } ///  /// Restore preserved values after the category has been re-created ///  static void SetPreservedValues(string category, Dictionary preservedValues) { foreach (KeyValuePair preservedValue in preservedValues) { PerformanceCounter performanceCounter = new PerformanceCounter(category, preservedValue.Key, false); performanceCounter.RawValue = preservedValue.Value; Console.WriteLine("Restoring {0} with a RawValue of {1}", preservedValue.Key, performanceCounter.RawValue); } }