你可以参考一下如下代码
[C#] 纯文本查看 复制代码
/// <summary>
/// 获取利用率
/// </summary>
/// <param name="lstCounters">计数集合</param>
/// <returns>利用率</returns>
public float GetUsage(List<PerformanceCounter> lstCounters)
{
if (null == lstCounters)
{
throw new ArgumentNullException("Parameter 'Counters' is null.");
}
//计算利用率
lstCounters.ForEach(a => a.NextValue());
Thread.Sleep(0x400);
return lstCounters.Sum(a => a.NextValue());
}
/// <summary>
/// 获取计数集合
/// </summary>
/// <param name="oCategory">种类</param>
/// <param name="iPID">进程ID</param>
/// <returns>计数集合</returns>
public List<PerformanceCounter> GetEngtypeCounters(PerformanceCounterCategory oCategory, int iPID = -1)
{
if (null == oCategory)
{
throw new ArgumentNullException("Parameter 'Category' is null.");
}
//获取指定的实例名
List<string> lstInstanceNames = new List<string>();
{
lstInstanceNames.AddRange(oCategory.GetInstanceNames());
};
//过滤
{
//排除非指定的实例名
if (iPID != -1)
{
lstInstanceNames.RemoveAll(p => !p.StartsWith(string.Format("pid_{0}", iPID)));
}
//排除非指定的实例名类型
{
lstInstanceNames.RemoveAll(p => !p.EndsWith("engtype_3D"));
}
}
//调试
{
foreach (var item in lstInstanceNames)
{
PerformanceCounter[] arr = oCategory.GetCounters(item);
foreach (var itemP in arr)
{
//Console.WriteLine(itemP.CounterName);
}
}
}
//获取利用率实例项
return lstInstanceNames.SelectMany(f => oCategory.GetCounters(f)).Where(f => "Utilization Percentage" == f.CounterName).ToList();
}
/// <summary>
/// 获取内存计数集合
/// </summary>
/// <param name="oCategory">种类</param>
/// <param name="iPID">进程ID</param>
/// <returns>内存计数集合</returns>
public List<PerformanceCounter> GetMemoryCounters(PerformanceCounterCategory oCategory, int iPID = -1)
{
if (null == oCategory)
{
throw new ArgumentNullException("Parameter 'Category' is null.");
}
//获取指定的实例名
List<string> lstInstanceNames = new List<string>();
{
lstInstanceNames.AddRange(oCategory.GetInstanceNames());
};
//过滤
{
//排除非指定的实例名
if (iPID != -1)
{
lstInstanceNames.RemoveAll(p => !p.StartsWith(string.Format("pid_{0}", iPID)));
}
}
//调试
{
foreach (var item in lstInstanceNames)
{
PerformanceCounter[] arr = oCategory.GetCounters(item);
foreach (var itemP in arr)
{
//Console.WriteLine(itemP.CounterName);
}
}
}
//获取利用率实例项
return lstInstanceNames.SelectMany(f => oCategory.GetCounters(f)).Where(f => "Local Usage" == f.CounterName).ToList();
}
/// <summary>
/// 获取种类
/// </summary>
/// <param name="strCategory">种类名</param>
/// <returns>种类</returns>
public PerformanceCounterCategory GetCategory(string strCategory)
{
if (string.IsNullOrEmpty(strCategory))
{
throw new ArgumentNullException("Parameter 'Category' is null.");
}
//调试
{
PerformanceCounterCategory[] arr = PerformanceCounterCategory.GetCategories(".");
foreach (var item in arr)
{
//Console.WriteLine(item.CategoryName);
}
}
//
PerformanceCounterCategory oCategory = null;
if (PerformanceCounterCategory.Exists(strCategory))
{
//获取存在的指定种类
oCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(f => f.CategoryName == strCategory);
}
if (null == oCategory)
{
//创建新的指定种类
oCategory = new PerformanceCounterCategory(strCategory);
}
return oCategory;
}
|