如何获取cpu频率,存储空间大小,磁盘剩余空间这类数据

默认分类 未结 1 1665
___Aueey
___Aueey 2023-03-22 13:42
相关标签:
1条回答
  • 2023-03-22 14:17
    在桌面,用鼠标右键点击我的电脑,选最后一项属性就可以看到CPU的频率和内存大小,磁盘空间把鼠标放到那个盘上就可以看到了。任务管理器1.初始化几个变量: this.cpu = new PerformanceCounter(Processor, % Processor Time, _Total); this.cinf = new ComputerInfo(); 2.获取占用CPU的百分比 public double GetCpuPercent() { var percentage = this.cpu.NextValue(); return Math.Round(percentage, 2, MidpointRounding.AwayFromZero); } 3.获取占用内存的百分比 public double GetMemoryPercent() { var usedMem = this.cinf.TotalPhysicalMemory - this.cinf .AvailablePhysicalMemory ;//总内存减去可用内存 return Math.Round( (double)(usedMem / Convert.ToDecimal(this.cinf.TotalPhysicalMemory) * 100), 2, MidpointRounding.AwayFromZero); } 4.获取磁盘剩余空间及总空间 /// summary /// 根据盘符获取磁盘信息 /// /summary /// param name=diskName/param /// returns一个自定义类对象/returns public HardDiskInfo GetHardDiskInfoByName(string diskName) { DriveInfo drive = new DriveInfo(diskName); return new HardDiskInfo { FreeSpace = GetDriveData(drive.AvailableFreeSpace),TotalSpace =GetDriveData (drive.TotalSize ),Name =drive .Name }; } /// summary /// 获取所有驱动盘信息 /// /summary /// returns/returns public IEnumerableHardDiskInfo GetAllHardDiskInfo() { ListHardDiskInfo list = new ListHardDiskInfo(); foreach (DriveInfo d in DriveInfo.GetDrives()) { if (d.IsReady) { list.Add(new HardDiskInfo { Name = d.Name, FreeSpace = this.GetDriveData(d.AvailableFreeSpace), TotalSpace = this.GetDriveData(d.TotalSize) }); } } return list; } private string GetDriveData(long data)//将磁盘大小的单位由byte转化为G { return (data / Convert.ToDouble(1024) / Convert.ToDouble(1024) / Convert.ToDouble(1024)).ToString(0.00); } class HardDiskInfo//自定义类 { public string Name { get; set; } public string FreeSpace { get; set; } public string TotalSpace { get;set;} }运行结果:下载鲁大师,检测配置您好,很高兴为您解答。this.cpu = new PerformanceCounter(Processor,%Processor Time,_Total); //初始化cpu和cinf变量this.cinf = new ComputerInfo();public double GetCpuPercent() //获取占用CPU的百分比{ var percentage = this.cpu.NextValue(): return Math.Round(percentage,2,MidpointRounding.AwayFromZero);}public double GetMemoryPercent() //获取占用内存的百分比{ var usedMem = this.cinf.TotalPhysicalMemory - this.cinf.AvailablePhysicalMemory; //总内存减去可用的内存 return Math.Round((double)(usedMem/Convert.ToDecimal(this.cinf.TotalPhysicalMemory)*100),2,MidpointRounding.AwayFromZero);}public HardDiskInfo GetHardDiskInfoByName(string diskName)//根据盘符获取磁盘信息{ DriveInfo drive = new DriveInfo(diskName); return new HardDiskInfo{ FereeSpace = GetDriveData(drive.AvailableFreeSpace), TotalSpace = GetDrieData(drive.TotalSizw),Name = drive.Name};}public IEnumerableHardDiskInfo GetAllHardDiskInfo() //获取所有驱动信息{ ListHardDiskInfo list = new ListHardDiskInfo(); foreach(DriverInfo d in DriveInfo.GetDrives()) { if(d.IsReady) { list.Add(new HardDiskInfo{ Name = d.Name, FreeSpace = this.GetDriveData(d.AvailableFreeSpace),TotalSpace = this.GetDriveData(d.TotalSize)}); } } return list;}private string GetDriveData(long data)//将磁盘大小的单位由byte转化为G{ return(data/Convert.ToDouble(1024)/Convert.ToDouble(1024)/Convert.ToDouble(1024).ToString(0.00);}calss HardDiskInfo//自定义类{ public string Name { get; set; } public string FreeSpace { get; set; } public string TotalSpace { get; set; }}如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】希望我的回答对您有所帮助,望采纳! ~ O(∩_∩)O~
    0 讨论(0)
提交回复