3.檢查驅動器A中磁盤是否有效
type
TDriveState = (DSNODISK, DSUNFORMATTEDDISK, DSEMPTYDISK, DSDISK_WITHFILES);
……
function DriveState (driveletter: Char) : TDriveState;
var
mask: String[6];
sRec: TSearchRec;
oldMode: Cardinal;
retcode: Integer;
begin
oldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
mask:= "?:\*.*";
mask[1] := driveletter;
{$I-}
retcode := FindFirst (mask, faAnyfile, Srec);
FindClose(Srec);
{$I+}
case retcode of
0 : Result := DSDISK_WITHFILES; //磁盤有文件
-18 : Result := DSEMPTYDISK; //好的空磁盤
-21, -3: Result := DSNODISK; //NT,Win31的錯誤代號
else
Result := DSUNFORMATTEDDISK;
end;
4. 獲取所有盤符和計算剩余
procedure TFileForm.GetDisks;
var
str:string;
Drivers:Integer;
driver:char;
i,temp,disksizee:integer;
d1,d2,d3,d4: DWord;
begin
Drivers:=GetLogicalDrives;
temp:=(1 and Drivers);
for i:=0 to 26 do
begin
if temp=1 then
begin
driver:=char(i+integer('A'));
str:=driver+':';
if (driver<>'') and (getdrivetype(pchar(str))<>drive_cdrom) and (getdrivetype(pchar(str))<>DRIVE_REMOVABLE) then
begin
GetDiskFreeSpace(pchar(str),d1,d2,d3,d4);
lbxDisks.Items.Add(str+Format('-----------總空間: %f GB',[d4/1024/1024/1024*d2*d1])+Format('--------剩余空間: %f GB',[d3/1024/1024/1024*d2*d1]));
end;
end;
drivers:=(drivers shr 1);
temp:=(1 and Drivers);
end;
end;
SetErrorMode(oldMode);
end;