WPF获得当前电脑的储存和运存

1.开门见山Show代码:

cs 复制代码
/// <summary>
/// 总内存(GB)
/// </summary>
public double TotalMemoryGB { get; set; }

/// <summary>
/// 已用内存(GB)
/// </summary>
public double UsedMemoryGB { get; set; }

/// <summary>
/// 总储存(GB)
/// </summary>
public double TotalStorageGB { get; set; }

/// <summary>
/// 已用储存(GB)
/// </summary>
public double UsedStorageGB { get; set; }

//储存
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
var driveLetter = Path.GetPathRoot(currentPath);
DriveInfo drive = new(driveLetter);
TotalStorageGB = Math.Round((double)drive.TotalSize / 1024 / 1024 / 1024, 2);
var UsedBytes = drive.TotalSize - drive.AvailableFreeSpace;
UsedStorageGB = Math.Round((double)UsedBytes / 1024 / 1024 / 1024, 2);

//内存
var _ramCounter = new PerformanceCounter("Memory", "Committed Bytes");
var _ramCounterSize = _ramCounter?.NextValue() ?? 0;
TotalMemoryGB = Math.Round((double)_ramCounterSize / 1024 / 1024 / 1024, 2);
var _availableCounter = new PerformanceCounter("Memory", "Available Bytes");
var _availableCounterSize = _availableCounter?.NextValue() ?? 0;
UsedMemoryGB = TotalMemoryGB - Math.Round((double)_availableCounterSize / 1024 / 1024 / 1024, 2);

*优化:字节数转GB可以单独成工具类方法

储存可以指定盘符,现在默认是运行程序根目录盘

相关推荐
samble1 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas1 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
dalong103 天前
WPF:Modbus 状态监控
wpf·modbus
He BianGu4 天前
【WPF-VisionMaster】机器视觉通用平台V5.0版本发行说明
opencv·c#·wpf·halcon·机器视觉·visionmaster
fogota7 天前
Emoji与Segoe MDL2 Assets的比较
c#·wpf
fogota7 天前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota7 天前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头7 天前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
gis开发之家8 天前
Spring Boot 4 定时任务与异步线程池:从 @Scheduled 到高并发任务编排(生产级实战)
java·spring boot·后端·wpf·spring boot4
clz13145219 天前
如何设计一个优雅可靠的金融级联机接口
wpf