procedure TForm1.Button1Click(Sender: TObject);
var
RootPath : string;
Sed_Cluster, Bytes_Sec, Free_Clusters, Total_Clusters : DWord;
begin
Memo1.Clear;
RootPath := 'C:/';
if RootPath<>'' then
begin
GetDiskFreeSpace(PChar(RootPath), Sed_Cluster, Bytes_Sec, Free_Clusters, Total_Clusters);
Memo1.Lines.Add('C盘总空间:'+FloatToStrF(Sed_Cluster* Bytes_Sec*Total_Clusters/1024/1024/1024,ffNumber,8,2)+'G');
Memo1.Lines.Add('C盘可用空间:'+FloatToStrF(Sed_Cluster* Bytes_Sec*Free_Clusters/1024/1024/1024,ffNumber,8,2)+'G');
end;
end;
GetDiskFreeSpace函数不适合磁盘空间超过2GB情况,在此情况应使用GetDiskFreeSpaceEx函数,参考笔者以下代码(GetDriveType函数可以读取磁盘类型):
procedure TForm1.Button1Click(Sender: TObject);
var
RootPath, DriveInfo : string;
Total_Space, Free_Available : Int64;
intDrvTypeID : integer;
dblFree, dblTotal : double;
begin
Memo1.Clear;
RootPath := UpperCase(Copy(DriveComboBox1.Text,1,1));
if RootPath<>'' then
begin
GetDiskFreeSpaceEx(PChar(RootPath + ':/'), Free_Available, Total_Space, nil);
DriveInfo := RootPath + '盘总空间为';
dblTotal := Total_Space/1024/1024/1024;
if dblTotal >= 1 then
DriveInfo := DriveInfo + FloatToStrF(dblTotal,ffNumber,8,2)+'GB。'
else
DriveInfo := DriveInfo + FloatToStrF(dblTotal*1024,ffNumber,8,0)+'MB。';
Memo1.Lines.Add(DriveInfo);
DriveInfo := RootPath + '盘可用空间为';
dblFree := Free_Available/1024/1024/1024;
if dblFree >= 1 then
DriveInfo := DriveInfo + FloatToStrF(dblFree,ffNumber,8,2)+'GB。'
else
DriveInfo := DriveInfo + FloatToStrF(dblFree*1024,ffNumber,8,0)+'MB。';
Memo1.Lines.Add(DriveInfo);
intDrvTypeID := GetDriveType(PChar(RootPath + ':/'));
case intDrvTypeID of
0 : Memo1.Lines.Add('无法获取磁盘类型。' );
1 : Memo1.Lines.Add('磁盘不存在。' );
DRIVE_REMOVABLE : Memo1.Lines.Add('类型:可移动磁盘。' );
DRIVE_FIXED : Memo1.Lines.Add('类型:本地磁盘。' );
DRIVE_REMOTE : Memo1.Lines.Add('类型:网络驱动器。' );
DRIVE_CDROM : Memo1.Lines.Add('类型:CDROM。' );
DRIVE_RAMDISK : Memo1.Lines.Add('类型:RAM存储器。' );
end;
end;
end;
使用API函数获取磁盘空间及磁盘类型
最新推荐文章于 2023-07-04 18:12:50 发布