程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi檢測磁盤數量得到磁盤空間大小

Delphi檢測磁盤數量得到磁盤空間大小

編輯:Delphi

Delphi獲取磁盤驅動器信息,程序自動檢測電腦的硬盤有幾個分區,每個磁盤分區的總容量大小,剩余空間大小等。通過下拉框可指定需要檢測的磁盤分區,然後通過下邊的按鈕進行檢測。程序代碼為:

vIEw source print? 01 unit MainUnit; 02 interface 03 uses 04   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 05   StdCtrls; 06 type 07   TMain = class(TForm) 08     StaticText1: TStaticText; 09     ComboBox1: TComboBox; 10     StaticText3: TStaticText; 11     Edit1: TEdit; 12     StaticText4: TStaticText; 13     Edit2: TEdit; 14     Button1: TButton; 15     ListBox1: TListBox; 16     procedure FormShow(Sender: TObject); 17     procedure Button1Click(Sender: TObject); 18   private 19     procedure GetDrivers; 20    { Private declarations } 21   public 22     { Public declarations } 23   end; 24 var 25   Main: TMain; 26   const dod= 1024*1024; 27 implementation 28 {$R *.DFM} 29 function DiskFree(Drive: String): Int64; 30 var 31  FreeBytesAvailableToCaller: Int64; 32  RootPtr: pChar; 33  TotalNumberOfBytes: Int64; 34 begin 35   RootPtr := Pchar(Drive); 36   if GetDiskFreeSpaceEx(RootPtr, 37                         FreeBytesAvailableToCaller, 38                         TotalNumberOfBytes, 39                         nil) 40   then RESULT := FreeBytesAvailableToCaller 41   else RESULT := -1 42 end; 43 function DiskSize(Drive: String): Int64; 44 var 45  FreeBytesAvailableToCaller: Int64; 46  RootPtr: pChar; 47  TotalNumberOfBytes: Int64; 48 begin 49   RootPtr := Pchar(Drive); 50   if   GetDiskFreeSpaceEx(RootPtr, 51                           FreeBytesAvailableToCaller, 52                           TotalNumberOfBytes, 53                           nil) 54     then RESULT := TotalNumberOfBytes 55     else RESULT := -1 56 end; 57 procedure TMain.GetDrivers; 58 var i, sResult: Integer; 59 begin 60   for i:= 0 to ListBox1.Items.Count-1 do 61     begin 62       sResult := GetDriveType(Pchar(ListBox1.Items[i] + ':\')); 63       if sResult= DRIVE_Fixed then 64         Combobox1.Items.Add(Listbox1.Items[i]+ ':\'); 65         Combobox1.Text:= Combobox1.Items[0]; 66     end; 67 end; 68 procedure TMain.FormShow(Sender: TObject); 69 begin 70   GetDrivers; 71 end; 72 procedure TMain.Button1Click(Sender: TObject); 73 var sType: String; 74 begin 75   if Combobox1.Text<> '' then 76     begin 77      sType:= Combobox1.Text; 78      Edit1.Text:= InttoStr(DiskSize(sType)div dod); 79      Edit2.Text:= InttoStr(DiskFree(sType)div dod); 80     end 81   else Showmessage('請選擇硬盤驅動器'); 82 end; 83 end.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved