1.讀硬盤序列號
function GetDiskSerial(DiskChar: Char): string;
var
SerialNum : pdword;
a, b : dword;
Buffer : array [0..255] of char;
begin
result := "";
if GetVolumeInformation(PChar(diskchar+":\"), Buffer, SizeOf(Buffer), SerialNum,
a, b, nil, 0) then
Result := IntToStr(SerialNum^);
end;
2.檢查磁盤是否就緒
Function DiskReady(Root: string) : Boolean;
var
Oem : CARDINAL ;
Dw1,Dw2 : DWORD ;
begin
Oem := SetErrorMode( SEM_FAILCRITICALERRORS ) ;
if LENGTH(Root) = 1 then Root := Root + ":\\" ;
Result := GetVolumeInformation( PCHAR( Root ), NIL,0,NIL, Dw1,Dw2, NIL,0 ) ;
SetErrorMode( Oem ) ;
end;
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;