Qt获取磁盘剩余空间

Qt获取磁盘剩余空间

  • Windows API —— GetDiskFreeSpaceEx()
    • 代码
    • 问题
    • 解决方案

Windows API —— GetDiskFreeSpaceEx()

代码

void ManagementWidget::slot_UpdateDiskSpace()
{
	QString strDisk = QCoreApplication::applicationDirPath().left(3);
	int dTotalBytes;
	int dFreeBytes;
	ULARGE_INTEGER lFreeBytesAvailable, lTotalBytesTemp, lTotalFreeBytes;
	if (!GetDiskFreeSpaceEx(strDisk.toStdString().c_str(), &lFreeBytesAvailable, &lTotalBytesTemp, &lTotalFreeBytes))
	{
		QMessageBox::warning(this, "Warning", "Acquire Disk Space Failed !");
		dTotalBytes = -1;
		dFreeBytes = -1;
		return;
	}

	//unit: GB
	double denominator = 1024.0 * 1024 * 1024;
	dTotalBytes = (int)(lTotalBytesTemp.QuadPart / denominator);
	dFreeBytes = (int)(lTotalFreeBytes.QuadPart / denominator);

	m_pLabel_DiskSpace->setText(tr("Disk Free Space: ") + QString("%1%").arg((float)((1000*dFreeBytes)/dTotalBytes)/10.0));
}

问题

使用VS2013运行,Debug模式报错,Release可以正常运行

解决方案

LPCWSTR drive = (LPCWSTR)strDisk.utf16(); 
if (!GetDiskFreeSpaceExW(drive, &lFreeBytesAvailable, &lTotalBytesTemp, &lTotalFreeBytes))
{
	QMessageBox::warning(this, "Warning", "Acquire Disk Space Failed !");
	dTotalBytes = -1;
	dFreeBytes = -1;
	return;
}

你可能感兴趣的:(常用工具,qt,开发语言)