GetDiskFreeSpace
Windows.Pas
Syntax
GetDiskFreeSpace(
lpRootPathName: PChar; {a pointer to the root path string}
var lpSectorsPerCluster: DWORD; {the number of sectors per cluster}
var lpBytesPerSector: DWORD; {the number of bytes per sector}
var lpNumberOfFreeClusters: DWORD; {the number of free clusters}
var lpTotalNumberOfClusters: DWORD {the total number of clusters on the drive}
): BOOL; {returns TRUE or FALSE}
Description
The GetDiskFreeSpace function retrieves information about a drive partition. It returns all the information needed to calculate the available free space for a drive. Note that under Windows 95 prior to service release 2, this function will return incorrect values for volumes bigger than 2 gigabytes in size.
Parameters
lpRootPathName: A null-terminated string containing the root directory of the drive to query.
lpSectorsPerCluster: A variable receiving the number of sectors per cluster for the specified drive. A sector is a collection of bytes, a cluster is a collection of sectors.
lpBytesPerSector: A variable receiving the number of bytes in each sector of the drive.
lpNumberOfFreeClusters: A variable receiving the number of free clusters on the drive. Drives are used in full cluster increments. Storing a file that is smaller then one cluster will allocate the entire cluster. To get the amount of free drive space in bytes, take the number of free clusters multiplied by the number of sectors per cluster, multiplied by the number of bytes per sector as in the following formula:
FreeClusters * SectorsPerCluster * BytesPerSector = Free space in bytes
lpTotalNumberOfClusters: A variable receiving the total number of clusters on the drive. To find the total size of the drive in bytes, take the total number of clusters multiplied by the number of sectors per cluster, multiplied by the number of bytes per sector, as in the following formula:
TotalNumberOfClusters * SectorsPerCluster * BytesPerSector = Size of drive in bytes
Return Value
If the function succeeds, it returns TRUE; otherwise it returns FALSE. To get extended error information, call the GetLastError function.
Example
Listing 14-5: Retrieving the Free Disk Space
procedure TForm1.Button1Click(Sender: Tobject);
var
SectorsPerCluster, // holds the sectors per cluster
BytesPerSector, // holds the bytes per sector
FreeClusters, // holds the number of free clusters
Clusters: DWORD; // holds the total number of disk clusters
begin
{retrieve the disk space information}
if GetDiskFreeSpace('C:\',SectorsPerCluster,BytesPerSector,
FreeClusters,Clusters) then
begin
{display the disk space information}
Panel2.Caption := IntToStr(SectorsPerCluster);
Panel3.Caption := IntToStr(BytesPerSector);
Panel4.Caption := IntToStr(FreeClusters);
Panel5.Caption := IntToStr(Clusters);
Panel6.Caption := IntToStr(FreeClusters*BytesPerSector*SectorsPerCluster);
Panel7.Caption := IntToStr(Clusters*BytesPerSector*SectorsPerCluster);
end;
end;
Snadji se! :)
Ivan :)