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

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

相关推荐
七夜zippoe15 小时前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang18 小时前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com21 小时前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf
武藤一雄2 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
炸炸鱼.2 天前
ELK 企业级日志分析系统完整部署手册
elk·wpf
Mr_pyx3 天前
微服务可观测性实战:分布式链路追踪从入门到精通
wpf
c#上位机4 天前
wpf附加事件
wpf
玖笙&4 天前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio
想你依然心痛4 天前
HarmonyOS 6(API 23)分布式实战:基于悬浮导航与沉浸光感的“光影协创“跨设备白板系统
分布式·wpf·harmonyos·悬浮导航·沉浸光感
c#上位机5 天前
wpf路由事件
wpf