C++Builder How to Get Disk Information in C++ on Windows?

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How to Get Disk Information in C++ on Windows?
By Yilmaz Yoru September 10, 2021

How can I get disk size information in C++ Builder? How can I get disk free size information in C++ Builder? How can I calculate free percentage of a disk? Let’s answer these questions.

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 Для просмотра ссылки Войди или Зарегистрируйся or we can use C++ standard library methods. In C++ Builder, we can use both of them and we can also use Для просмотра ссылки Войди или Зарегистрируйся to create or remove directories or to get information about disks, directories and files. These methods are easy to remember, very friendly, and smart.

Let’s see how we can get disk size and disk free size in C++ Builder

How do I get the free space size for a disk in C++ Builder?​

Use the Для просмотра ссылки Войди или Зарегистрируйся method (System::SysUtils::DiskFree) which is a SysUtils Method listed in Для просмотра ссылки Войди или Зарегистрируйся. 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 DiskFree method is only available on Windows.

Here is the DiskFree Method in C++ Builder:
C++:
__int64 __fastcall DiskFree(System::Byte Drive);
Here is a simple example to DiskFree() Method in C++ Builder,
C++:
__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 Для просмотра ссылки Войди или Зарегистрируйся. 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.
C++:
__int64 __fastcall DiskSize(System::Byte Drive);
Here is a simple example to DiskSize() Method in C++ Builder,
C++:
__int64 disk_size = DiskSize(0);

Is there a full example of using the DiskSize and DiskFree methods in C++?​

C++:
#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);
}