using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Runtime.InteropServices; namespace Common { public class CPUData { public static PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); public static double GetCPU() { cpuCounter.NextValue(); // 初始化计数器,让它开始计数 System.Threading.Thread.Sleep(1000); // 等待一秒 double cpuUsage = cpuCounter.NextValue(); // 获取CPU使用率 return cpuUsage; } [DllImport("kernel32.dll")] private static extern void GetSystemTimePreciseAsFileTime(out long fileTime); // 将 FILETIME (long) 转换为 DateTime public static DateTime GetNowPrecise() { long fileTime; GetSystemTimePreciseAsFileTime(out fileTime); DateTime localTime = DateTime.FromFileTimeUtc(fileTime).ToLocalTime(); return localTime; } } }