delphi读取磁盘信息

delphi之磁盘篇

  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;

posted @ 2008-10-14 14:22  苔苔以苔苔以苔  阅读(912)  评论(0编辑  收藏  举报
猪先飞