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

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

相关推荐
武藤一雄8 小时前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
CSharp精选营14 小时前
都是微软亲儿子,WPF凭啥干不掉WinForm?这3个场景说明白了
c#·wpf·跨平台·winform
baivfhpwxf202314 小时前
wpf TextBlock 控件如何根据内容换行?
wpf
亘元有量-流量变现15 小时前
鸿蒙、安卓、苹果音频设备技术深度解析与开发实践
android·wpf·harmonyos·亘元有量·积分墙
软泡芙15 小时前
【Bug】ReactiveUI WPF绑定中依赖属性不更新的问题分析与解决方案
java·bug·wpf
浪扼飞舟15 小时前
WPF输入验证(ValidationRule)
java·javascript·wpf
IOFsmLtzR2 天前
Flink Agents 源码解读 --- (5) --- ActionExecutionOperator
microsoft·flink·wpf
廋到被风吹走4 天前
【AI】Codex 复杂任务拆解:从“一气呵成“到“步步为营“
人工智能·wpf
希望永不加班4 天前
SpringBoot 整合 Redis 缓存
spring boot·redis·后端·缓存·wpf
_MyFavorite_4 天前
JAVA重点基础、进阶知识及易错点总结(29)JDK8 时间 API 进阶
java·开发语言·wpf