Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Disk free space

0.00/5 (No votes)
6 Apr 2003 1  
This code is used to check for free space available on logical disks

Sample Image - Harddisk.jpg

Introduction

This code is used to check for free space available on logical disks.

How to use it

Copy the source files HardDiskManager.cpp and HardDiskManager.h to your project directory and add them to your project.

CHardDiskManager::CHardDiskManager()
{
    // bytes available to caller

    m_uliFreeBytesAvailable.QuadPart = 0L;

    // bytes on disk

    m_uliTotalNumberOfBytes.QuadPart = 0L;

    // free bytes on disk

    m_uliTotalNumberOfFreeBytes.QuadPart = 0L;
}

CHardDiskManager::~CHardDiskManager()
{
}

bool CHardDiskManager::CheckFreeSpace(LPCTSTR lpDirectoryName)
{
    if( !GetDiskFreeSpaceEx(
        lpDirectoryName,                  // directory name

        &m_uliFreeBytesAvailable,         // bytes available to caller

        &m_uliTotalNumberOfBytes,         // bytes on disk

        &m_uliTotalNumberOfFreeBytes) )   // free bytes on disk

        return false;

    return true;
}

DWORD64 CHardDiskManager::GetFreeBytesAvailable(void)
{ 
    return m_uliFreeBytesAvailable.QuadPart;
}

DWORD64 CHardDiskManager::GetTotalNumberOfBytes(void)
{ 
    return m_uliTotalNumberOfBytes.QuadPart;
}

DWORD64 CHardDiskManager::GetTotalNumberOfFreeBytes(void)
{ 
    return m_uliTotalNumberOfFreeBytes.QuadPart;
}

double CHardDiskManager::GetFreeGBytesAvailable(void)
{ 
    return (double)(
        (signed __int64)(m_uliFreeBytesAvailable.QuadPart) /
        1.0e9
    );
}

double CHardDiskManager::GetTotalNumberOfGBytes(void)
{ 
    return (double)(
        (signed __int64)(m_uliTotalNumberOfBytes.QuadPart) /
        1.0e9
    );
}

double CHardDiskManager::GetTotalNumberOfFreeGBytes(void)
{ 
    return (double)(
        (signed __int64)(m_uliTotalNumberOfFreeBytes.QuadPart) /
        1.0e9
    );
}

//

// Application

//

#include "HardDiskManager.h"


using namespace std;


void main( int argc, char** argv ) 
{
    CHardDiskManager manager;

    // Set the directory name with " : " 

    // Ex:. C: , c: , F: , ...

    manager.CheckFreeSpace("c:");

    cout << "Drive C:\\" << endl;

    cout << "FreeBytesAvailable:     " <<
        manager.GetFreeGBytesAvailable() <<
        " GBytes. " << endl;
    
    cout << "TotalNumberOfBytes:     " <<
        manager.GetTotalNumberOfGBytes()
        << " GBytes. " << endl;

    cout << "TotalNumberOfFreeBytes: " <<
        manager.GetTotalNumberOfFreeGBytes() <<
        " GBytes. " << endl;

    manager.CheckFreeSpace("d:");

    cout << "Drive D:\\" << endl;

    cout << "FreeBytesAvailable:     " <<
        manager.GetFreeGBytesAvailable()
        << " GBytes. " << endl;

    cout << "TotalNumberOfBytes:     " <<
        manager.GetTotalNumberOfGBytes()
        << " GBytes. " << endl;

    cout << "TotalNumberOfFreeBytes: " <<
        manager.GetTotalNumberOfFreeGBytes() <<
        " GBytes. " << endl;

    return 0;
}

///////////////////////////////////////////////////////////////

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here