ComputerStatusTool.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Linq;
  11. using System.Text;
  12. namespace Azylee.Core.WindowsUtils.InfoUtils
  13. {
  14. public class ComputerStatusTool
  15. {
  16. /// <summary>
  17. /// 获取 Processor(可获取CPU使用率)
  18. /// </summary>
  19. /// <returns></returns>
  20. public static PerformanceCounter Processor()
  21. {
  22. PerformanceCounter processor = null;
  23. try
  24. {
  25. processor = new PerformanceCounter("Processor", "% Processor Time", "_Total");
  26. }
  27. catch { }
  28. return processor;
  29. }
  30. public static bool TryGetNextValue(PerformanceCounter p, out float value)
  31. {
  32. value = 0;
  33. try
  34. {
  35. if (p != null)
  36. {
  37. value = p.NextValue();
  38. return true;
  39. }
  40. return false;//性能计数器为空,返回失败
  41. }
  42. catch
  43. {
  44. return false;//异常,返回失败
  45. }
  46. }
  47. }
  48. }