ディスクの容量を取得する

ディスクの空き容量・全容量を得るには GetDiskFreeSpace APIを使用します。バイト単位で戻ってくるのでメガバイトにする場合は1024で2回割ってください。関数が失敗するとFalseを返します。以下の例は Dドライブの全容量・空き容量をラベルに表示します。

■ ディスクの容量を取得する例
var
  intSecCls   :  integer;  //いちクラス中のセクタ数
  intBytSec   :  integer;  //いちセクタ中のバイト数
  intFreeCls  :  integer;  //空きのクラスタ数
  intAllCls   :  integer;  //全クラスタ数
  intFree     :  integer;  
  intAll      :  integer; 
begin
  if  GetDiskFreeSpace(Pchar('D:\'),intSecCls,intBytSec,intFreeCls,intAllCls) then 
  begin
      //空き容量計算
      intFree := (intBytSec * intSecCls) * intFreeCls;
      //全容量計算
      intAll  := (intBytSec * intSecCls) * intAllCls;
      //MBに変換
      intFree := (intFree div 1024) div 1024;
      intAll  := (intFree div 1024) div 1024;
      //ラベルに表示
      Label1.Caption  :=  IntToStr(intFree) + '/' + IntToStr(intAll);
  end;
end.