当前位置:网站首页>C# 读取内存条占用大小,硬盘占用大小
C# 读取内存条占用大小,硬盘占用大小
2022-06-23 21:56:00 【熊思宇】
先建一个读取系统数据工具类
using System;
using System.Management;
namespace Utils
{
public class SystemInfo
{
#region 字段定义
public static SystemInfo Instance;
private long m_PhysicalMemory = 0;
/// <summary>
/// 获取物理内存
/// </summary>
public long PhysicalMemory { get => m_PhysicalMemory; }
/// <summary>
/// 获取可用内存
/// </summary>
public long MemoryAvailable
{
get
{
long availablebytes = 0;
ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject mo in mos.GetInstances())
{
if (mo["FreePhysicalMemory"] != null)
{
availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
}
}
return availablebytes;
}
}
#endregion
#region 构造函数
static SystemInfo()
{
Instance = new SystemInfo();
}
private SystemInfo()
{
//获得物理内存
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["TotalPhysicalMemory"] != null)
{
m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString());
}
}
}
#endregion
/// <summary>
/// 获取内存大小
/// </summary>
/// <returns>
/// 返回值
/// 参数1代表内存已使用大小,
/// 参数2代表内存总大小,
/// 参数3代表已使用占用百分比
/// </returns>
public Tuple<double, double, double> GetMemorySize()
{
double used = (PhysicalMemory - MemoryAvailable) / 1024.0 / 1024.0 / 1024.0;//已用内存
double physicalMemory = PhysicalMemory / 1024.0 / 1024.0 / 1024.0;//物理内存总量
double proportion = (PhysicalMemory - MemoryAvailable) * 100.0 / PhysicalMemory;
used = Math.Round(used, 2);
physicalMemory = Math.Round(physicalMemory, 2);
proportion = (int)proportion;
return new Tuple<double, double, double>(used, physicalMemory, proportion);
}
/// <summary>
/// 获取指定磁盘空间到大小
/// </summary>
/// <param name="hardDiskName"></param>
/// <returns>
/// 返回值
/// 参数1代表磁盘已使用大小,
/// 参数2代表磁盘总大小
/// </returns>
public Tuple<long, long> GetHardDiskSize(string hardDiskName)
{
long freeSpace = new long();
long totalSize = new long();
hardDiskName = hardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == hardDiskName)
{
freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
totalSize = drive.TotalSize / (1024 * 1024 * 1024);
}
}
return new Tuple<long, long>(freeSpace, totalSize);
}
}
}
调用:
Tuple<long, long> tuple1 = SystemInfo.Instance.GetHardDiskSize("C");
Console.WriteLine(string.Format("硬盘剩余大小:{0}GB,总大小:{1}GB", tuple1.Item1, tuple1.Item2));
Tuple<double, double, double> tuple2 = SystemInfo.Instance.GetMemorySize();
Console.WriteLine(string.Format("获取内存大小,剩余大小:{0}GB,总大小:{1}GB,占用百分比:{2}%", tuple2.Item1, tuple2.Item2, tuple2.Item3));
输出:

end
边栏推荐
- The principle of async and await
- The Sandbox 归属周来啦!
- FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
- 【设计】1359- Umi3 如何实现插件化架构
- ASM文件系统 数据如何写和读数据
- MySQL highly available version 1c1g exclusive cloud database value-added special offers!
- C#/VB.NET Word转Text
- Summary of cloud native pipeline tools
- PostgreSQL怎么创建分区表详解
- How PostgreSQL creates partition tables
猜你喜欢

【技术干货】蚂蚁办公零信任的技术建设路线与特点

Save: software analysis, verification and test platform

巨头下场“摆摊”,大排档陷入“苦战”

数据解读!理想L9冲刺「月销过万」,从BBA手中抢份额

迪赛智慧数——柱状图(基本柱状图):2022年父亲节过节的方式

Desai wisdom number - histogram (basic histogram): the way to celebrate father's day in 2022
Trigger definition and syntax introduction in MySQL

C#/VB. Net word to text

WebService客户端请求失败 can not create a secure xmlinputfactory

什么是免疫组织化学实验? 免疫组织化学实验
随机推荐
FANUC机器人SRVO-050碰撞检测报警原因分析及处理对策(亲测可用)
The Sandbox 归属周来啦!
反序列化——php反序列化
评估和选择最佳学习模型的一些指标总结
国家邮政局等三部门:加强涉邮政快递个人信息安全治理,推行隐私面单、虚拟号码等个人信息去标识化技术
Production of labels for table products
What is the development prospect of face recognition technology?
How to batch generate flattermark barcode
Three ways to enable IPv6 on Tencent cloud
Recommended | January activity 2-core 4G lightweight application server, enterprise nationwide purchase 6.7 yuan / month!!!
MySQL事務隔離
Summary of cloud native pipeline tools
sql server常用sql
Postman可以集成到CI,CD流水线中做自动化接口测试吗?
C#/VB.NET Word转Text
The latest February activity # 1 core 2G first year: 38 yuan / year! 2-core 4G light weight RMB 74 / year! Mysql database 19.9 yuan / year!!
Deserialization - PHP deserialization
How PostgreSQL creates partition tables
如何利用数仓创建时序表
迪赛智慧数——柱状图(基本柱状图):2022年父亲节过节的方式