一.目的
QT代码实现windows下获取CPU、内存及磁盘信息。
二.代码实现
1.获取CPU和内存信息
#include <Windows.h>
// 获取CPU信息
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
qDebug() << "CPU Architecture:" << (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ? "AMD64" : "x86");
qDebug() << "Number of Processors:" << systemInfo.dwNumberOfProcessors;
// 获取内存信息
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
qDebug() << "Total Physical Memory:" << statex.ullTotalPhys / (1024.0 * 1024.0) << "MB";
qDebug() << "Available Physical Memory:" << statex.ullAvailPhys / (1024.0 * 1024.0) << "MB";
2.获取磁盘信息
#include <QStorageInfo>
//获取当前系统盘的磁盘情况
QStorageInfo storageInfo = QStorageInfo::root();
storageInfo.refresh(); // 刷新信息,获取最新的磁盘信息
qDebug() << "总大小:" << storageInfo.bytesTotal() / (1024 * 1024 * 1024) << "GB";
qDebug() << "可用空间:" << storageInfo.bytesAvailable() / (1024 * 1024 * 1024) << "GB";
// 获取指定路径的磁盘情况
QString path = "D:/"; // 替换为你想查询的磁盘或目录路径
QList<QStorageInfo> storageInfos = QStorageInfo::mountedVolumes();
for (const QStorageInfo &storageInfo : storageInfos) {
if (storageInfo.rootPath() == path) {
qDebug() << "Total Disk Space:" << storageInfo.bytesTotal() / (1024.0 * 1024.0) << "MB";
qDebug() << "Available Disk Space:" << storageInfo.bytesAvailable() / (1024.0 * 1024.0) << "MB";
qDebug() << "Device Name:" << storageInfo.name();
qDebug() << "File System Type:" << storageInfo.fileSystemType();
break; // 找到匹配的存储设备后退出循环
}
}