Delphi獲取磁盤驅動器信息,程序自動檢測電腦的硬盤有幾個分區,每個磁盤分區的總容量大小,剩余空間大小等。通過下拉框可指定需要檢測的磁盤分區,然後通過下邊的按鈕進行檢測。程序代碼為:
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
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
43
function DiskSize(Drive: String): Int64;
44
45
46
47
48
49
50
51
52
53
54
then RESULT := TotalNumberOfBytes
55
56
57
procedure TMain.GetDrivers;
58
var i, sResult: Integer;
59
60
for
i:= 0 to ListBox1.Items.Count-1
do
61
62
sResult := GetDriveType(Pchar(ListBox1.Items[i] + ':\'));
63
sResult= DRIVE_Fixed then
64
Combobox1.Items.Add(Listbox1.Items[i]+ ':\');
65
Combobox1.Text:= Combobox1.Items[0];
66
67
68
procedure TMain.FormShow(Sender: TObject);
69
70
GetDrivers;
71
72
procedure TMain.Button1Click(Sender: TObject);
73
var sType: String;
74
75
Combobox1.Text<>
''
then
76
77
sType:= Combobox1.Text;
78
Edit1.Text:= InttoStr(DiskSize(sType)div dod);
79
Edit2.Text:= InttoStr(DiskFree(sType)div dod);
80
end
81
Showmessage(
'請選擇硬盤驅動器'
);
82
83
end.
Start a new Java project b
本例效果圖:unit Unit1; interface
代碼: unit Unit1; interface
本例效果圖:代碼文件:unit Unit1;i
和在 Net 中不同的是, 在具體指定圖像格式時, 這裡常