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可以单独成工具类方法

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

相关推荐
心平气和量大福大7 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
listening77716 小时前
HarmonyOS 6.1 全场景协同实战:从“单机”到“超级终端”的分布式重构
wpf
心平气和量大福大1 天前
C#-WPF-布局-Grid
c#·wpf·visual studio
心平气和量大福大2 天前
C#-WPF-Window主窗体
开发语言·c#·wpf
FuckPatience3 天前
Telerik UI for WPF 值不能为null。参数名:key
ui·wpf
lindexi3 天前
WPF 笔迹延迟优化从硬件到软件的全链路分析
wpf
weixin_727535624 天前
双Token认证体系深度拆解:Spring Security + JWT + Redis
redis·spring·wpf
liuxiaowei34 天前
Winform+WPF双框架实战:喷涂工艺SCADA上位机从0到1搭建(附采集监控源码+车间踩坑实录)
大数据·hadoop·wpf
贺国亚5 天前
模型训练-分布式与GPU调度
分布式·wpf
不羁的木木5 天前
HarmonyOS技术精讲-Connectivity Kit:实战——多屏协同与文件快传应用
华为·wpf·harmonyos