#include <sys/vfs.h> #include <sys/stat.h>qulonglong getFreeSpace(const QString &path) { QString apath = QDir(path).absolutePath(); struct stat stst; struct statfs stfs; if (::stat(apath.toLocal8Bit(),&stst) == -1) { return 0; } if (::statfs(apath.toLocal8Bit(),&stfs) == -1) { return 0; } return stfs.f_bavail * (stst.st_blksize); }
C++ (Qt)#ifdef _WIN32 //Для WIN32 #include <windows.h>#else //Для Linux #include <sys/vfs.h> #include <sys/stat.h>#endif //WIN32
C++ (Qt)qulonglong AppArguments::getFreeSpace(const QString &path){#ifdef _WIN32 QString sCurDir = QDir::current().absPath(); QDir::setCurrent( path ); double fTotal; double fFree; ULARGE_INTEGER free,total; bool bRes = ::GetDiskFreeSpaceExA( 0 , &free , &total , NULL ); if ( !bRes ) return false; QDir::setCurrent( sCurDir ); fFree = static_cast<__int64>(free.QuadPart)); //fTotal = static_cast<double>( static_cast<__int64>(total.QuadPart) ) / fKB; return fFree;#else //linux QString apath = QDir(path).absolutePath(); struct stat stst; struct statfs stfs; if (::stat(apath.toLocal8Bit(),&stst) == -1) { return 0; } if (::statfs(apath.toLocal8Bit(),&stfs) == -1) { return 0; } return stfs.f_bavail * (stst.st_blksize);#endif // _WIN32}
C++ (Qt)bool getDiskFreeSpace (const QString& path, qint64& total, qint64& free, qint64& available) const{ return GetDiskFreeSpaceEx((wchar_t*) path.utf16(), (PULARGE_INTEGER)&available , (PULARGE_INTEGER)&total , (PULARGE_INTEGER)&free );}
C++ (Qt)bool getDiskFreeSpace(const QString& path, qint64& total, qint64& free, qint64& available) const{ struct statvfs st; if(statvfs(QFile::encodeName(path).data(), &st) == 0) { total = st.f_frsize * st.f_blocks; free = st.f_frsize * st.f_bfree; available = st.f_frsize * st.f_bavail; return true; } return false;}