Get free disk space
-
Hello,
i am looking for a function to get free disk space on a selected volume, but i can't find none native.
So i found this:
http://stackoverflow.com/questions/1732717/how-to-determine-how-much-free-space-on-a-drive-in-qt
(ticked answer). I like the solution, because the application will run on windows. But what headers do i have to add? What lines in the .pro ? Thank you!
This is the API http://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx -
There's a "QtMobility::QSystemStorageInfo":http://doc.qt.digia.com/qtmobility/qsystemstorageinfo.html#totalDiskSpace class for that if you're willing to use QtMobility.
As for the Windows api there's always info about what headers and libs the function lives in at the bottom of the doc.
In your case it says:Header: FileAPI.h (include Windows.h); so you include Windows.h somewhere where you need the api.
Library: Kernel32.lib so you add LIBS += -lkernel32 in your .pro file
-
I tried to use qtmobility, but qtcreator was not unable to find the headers.
Using the windows API fixed the problem.
NOTE:
the windows API requires a WCHAR. Use QString::toWcharArray()
to convert a QString to WCHAR. -
Sometimes when I need to quickly pass a QString into the windows apis I use mystr.toStdWString().c_str(). Saves me writing the two lines to create an array. Of course the pointer becomes invalid right after the call so don't store it, but it can be safely passed into the function.
-
Have a look at "this thread":http://qt-project.org/forums/viewthread/27523/. Aside from "QtMobility::QSystemStorageInfo":http://doc.qt.digia.com/qtmobility/qsystemstorageinfo.html#totalDiskSpace you could also use "boost:filesystem":http://www.boost.org/doc/libs/1_53_0/libs/filesystem/doc/index.htm
@
boost::filesystem::path p (filepath);
boost::filesystem::space_info s = boost::filesystem::space(p);
quint64 uFreeStorage = s.available;
quint64 uTotalStorage = s.capacity;
@And then of course there are also the native functions, but I would always prefer a platform independent solution.
-
or use QtSystems module's "QStorageInfo":http://qt.gitorious.org/qt/qtsystems/blobs/dev/src/systeminfo/qstorageinfo.h class
-
QStorageInfo is good! However i am not able to use it. I added in my project
#include "systeminfo/qstorageinfo.h"But when compiling my project i get:
no such file or directory QtSystemInfo/qsysteminfoglobal.h -
It's not part of the official Qt package. It's a separate module that you need to build yourself.
-
Ok , i got the complete package, and opened the qtsystem.pro
Configured to build with qt 5.0.2 and runned qmake:
Failed to run: C:\Qt\Qt5.0.2\5.0.2\mingw47_32\bin\syncqt.bat -module QtPublishSubscribe -version 5.2.0 -outdir C:/Users/Marco/Desktop/build-qtsystems-Desktop_Qt_5_0_2_MinGW_32bit-Debug C:/Users/Marco/Desktop/qt-qtsystemsI can't understand how to fix this.
-
Had you installed perl ?
-
Hi,
this is a simple code to find out :- the total space of a disk
- the freespace of a disk
I am not using boost or other Qt modules. I am using MSDN features.
You need to include
@
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
@Just test is in a simple => main.cpp or is in a simple class to test it
@
BOOL fResult;wchar_t * pszDrive = L"e:\"; // here you can change the letter of your drive.
qint64 i64FreeBytesToCaller,
i64TotalBytes,
i64FreeBytes;fResult = GetDiskFreeSpaceEx (pszDrive,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
printf ("Available space to caller = %I64u MB\n",
i64FreeBytesToCaller) / (10241024));
printf ("Total space = %I64u MB\n",
i64TotalBytes) / (10241024));
printf ("Free space on drive = %I64u MB\n",
i64FreeBytes) / (1024*1024));
}if (!fResult)
printf ("error: %lu: could not get free space for "%s"\n",
GetLastError(), argv[1]);
@if you want to use a QString for the letter of the drive, just do those two conversions :
@
// m_diskLetter is a QString
std::wstring ws = m_diskLetter->toStdWString();
const wchar_t * pszDrive = ws.c_str(); // pszDrive is the letter of the drive for the fonction
@