C++ has a lot of great libraries to operate on files, folders, and disks. We can create or delete directories by using System Commands that we explained before in this post or we can use C++ standard library methods. In C++ Builder, we can use both of them and we can also use Disk And Directory Support Routines to create or remove directories or to get information about disks, directories, and files. These methods are easy to remember, very friendly, and smart.
In this post, you’ll learn how to get disk size and disk free size information in C++ Builder, as well as how to calculate a disk’s free percentage. By learning how to use disk size and disk free size in C++ Software. It will help you to easily build C++ applications.
How do I get the free space size for a disk in C++ Builder?
Use the DiskFree method (System::SysUtils::DiskFree) which is a SysUtils Method listed in Disk And Directory Support Routines. It returns the number of free bytes on a specified drive. The DiskFree method returns the number of free bytes on the specified drive, where 0 = Current, 1 = A, 2 = B, and so on. DiskFree returns -1 if the drive number is invalid. Note that the DiskFree method is only available on Windows.
Here is the DiskFree Method in C++ Builder:
1 2 3 |
__int64 __fastcall DiskFree(System::Byte Drive); |
Here is a simple example of DiskFree() Method in C++ Builder,
1 2 3 |
__int64 free = DiskFree( 0 ); |
How do I get the total size of a disk in C++ Builder?
Use the DiskSize mMethod (System::SysUtils::DiskSize) is a SysUtils Method listed in Disk And Directory Support Routines. It returns the total size, in bytes, of a specified drive. DiskSize returns the size in bytes of the specified drive, where 0 = Current, 1 = A, 2 = B, and so on. DiskSize returns -1 if the drive number is invalid. Note that DiskSize is only available on Windows.
1 2 3 |
__int64 __fastcall DiskSize(System::Byte Drive); |
Here is a simple example to DiskSize() Method in C++ Builder,
1 2 3 |
__int64 disk_size = DiskSize(0); |
Is there a full example of using the DiskSize and DiskFree methods in C++?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <vcl.h> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { __int64 disk_size = DiskSize(0); __int64 disk_free = DiskFree(0); String str; str.printf( L"%I64d percent of the space on drive 0 is free: %I64d KB", disk_free*100/disk_size, disk_free/1024 ); ShowMessage(str); } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition