在DELPHI中獲得磁盤容量

使用如下API函數

BOOL GetDiskFreeSpace(

LPCTSTR lpRootPathName, // address of root path

LPDWORD lpSectorsPerCluster, // address of sectors per cluster

LPDWORD lpBytesPerSector, // address of bytes per sector

LPDWORD lpNumberOfFreeClusters, // address of number of free clusters

LPDWORD lpTotalNumberOfClusters // address of total number of clusters

);

Parameters

lpRootPathName

Points to a null-terminated string that specifies the root directory of the disk to return information about. If lpRootPathName

is NULL, the function uses the root of the current directory.

lpSectorsPerCluster

Points to a variable for the number of sectors per cluster.

lpBytesPerSector

Points to a variable for the number of bytes per sector.

lpNumberOfFreeClusters

Points to a variable for the total number of free clusters on the disk.

lpTotalNumberOfClusters

Points to a variable for the total number of clusters on the disk.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

例子:

procedure TForm1.Button1Click(Sender: TObject);

var DriveString:String;

sec1, byt1, cl1, cl2: LongWord;

Disk_FreeSpace : real;

begin

GetDiskFreeSpace('d:\', sec1, byt1, cl1, cl2);

Disk_FreeSpace := (cl1 / (1024*1024*1024))*sec1*byt1;

showmessage(format('該驅動器容量是%0.3fG',[Disk_FreeSpace]));

end;

上面的程序是將數據從字節單位轉換爲G的,之所以這樣做,是爲了避免當磁盤容量大于DELPHI基本數據類型所能存儲的最大值,避免溢出。如果想獲得以字節爲單位的,那麽將遇到大數相乘的問題。

下面提供一個大數相乘的算法,他接收兩個字符串,輸出這個兩個字符串的乘積(當然字符串裏都是數字)

function TForm1.XAddY(x, y: string): string;

var

a,b,c:array[1..1000] of integer;

i,j,k,l,m,code:integer;

s,p,r:string;

begin

s := x; //兩個要相乘的字符串

p := y;

l:=length(s);

for i:=l downto 1 do

Val(s[i],a[l-i+1],code);

m:=length(p);

for i:=m downto 1 do

Val(p[i],b[m-i+1],code);

for j:=1 to m do

for i:=1 to l do

begin

if c[i+j-1]+a[i]*b[j]<=9 then begin

c[j+i-1]:=c[i+j-1]+a[i]*b[j];

k:=i+j-1;

end else begin

c[j+i-1]:=c[i+j-1]+(a[i]*b[j]) mod 10;

c[j+i]:=c[j+i]+ c[j+i-1] div 10+ (a[i]*b[j]) div 10;

c[i+j-1]:=c[i+j-1] mod 10;

k:=i+j;

end;

end;

r := '';

for i:=k downto 1 do

r := r+IntToStr(c[i]);

Result := r;

end;

下面我們就可以通過使用大數相乘的算法的得到磁盤的容量(用字節表示)

procedure TForm1.Button2Click(Sender: TObject);

var

sec1, byt1, cl1, cl2: LongWord;

Disk_FreeSpace : string;

begin

GetDiskFreeSpace('d:\', sec1, byt1, cl1, cl2);

Disk_FreeSpace := XAddY(inttostr(cl1),inttostr(sec1*byt1));

showmessage(format('該驅動器容量是%s字節',[Disk_FreeSpace]));

end;

程序完整的代碼如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Label1: TLabel;

Label2: TLabel;

Button2: TButton;

Label3: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

function XAddY(x : string;y:string) : string;

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var DriveString:String;

sec1, byt1, cl1, cl2: LongWord;

Disk_FreeSpace : real;

begin

GetDiskFreeSpace('d:\', sec1, byt1, cl1, cl2);

Disk_FreeSpace := (cl1 / (1024*1024*1024))*sec1*byt1;

showmessage(format('該驅動器容量是%0.3fG',[Disk_FreeSpace]));

end;

function TForm1.XAddY(x, y: string): string;

var

a,b,c:array[1..1000] of integer;

i,j,k,l,m,code:integer;

s,p,r:string;

begin

s := x; //兩個要相乘的字符串

p := y;

l:=length(s);

for i:=l downto 1 do

Val(s[i],a[l-i+1],code);

m:=length(p);

for i:=m downto 1 do

Val(p[i],b[m-i+1],code);

for j:=1 to m do

for i:=1 to l do

begin

if c[i+j-1]+a[i]*b[j]<=9 then begin

c[j+i-1]:=c[i+j-1]+a[i]*b[j];

k:=i+j-1;

end else begin

c[j+i-1]:=c[i+j-1]+(a[i]*b[j]) mod 10;

c[j+i]:=c[j+i]+ c[j+i-1] div 10+ (a[i]*b[j]) div 10;

c[i+j-1]:=c[i+j-1] mod 10;

k:=i+j;

end;

end;

r := '';

for i:=k downto 1 do

r := r+IntToStr(c[i]);

Result := r;

end;

procedure TForm1.Button2Click(Sender: TObject);

var

sec1, byt1, cl1, cl2: LongWord;

Disk_FreeSpace : string;

begin

GetDiskFreeSpace('d:\', sec1, byt1, cl1, cl2);

Disk_FreeSpace := XAddY(inttostr(cl1),inttostr(sec1*byt1));

showmessage(format('該驅動器容量是%s字節',[Disk_FreeSpace]));

end;

end.

 
關于磁盤容量和可用容量
1. 關于磁盤容量,電腦裏的算法是:1M=1024K,而手機或是其它的算法是1M=1000K。由此就造成了可用空間與所標示的有一點差別。 2.磁盤在進行了多次的文件交換過後,磁盤的總容量會有所減少,這一般是由于磁盤錯誤...查看完整版>>關于磁盤容量和可用容量
 
WindowsXP取消磁盤容量警告
     當發布Windows XP操作系統的時候,微軟一直努力去做的就是想方設法讓其傻瓜化——增加了各種各樣的鈴聲和口哨聲以提示用戶使其免受各種麻煩困擾。其中一種便是磁盤容量不足提示警告(Low Disk Sp...查看完整版>>WindowsXP取消磁盤容量警告
 
如何取消Windows系統磁盤容量警告
  當發布Windows XP操作系統的時候,微軟一直努力去做的就是想方設法讓其傻瓜化――增加了各種各樣的鈴聲和口哨聲以提示用戶使其免受各種麻煩困擾。其中一種便是磁盤容量不足提示警告(Low Disk Space Notificatio...查看完整版>>如何取消Windows系統磁盤容量警告
 
如何取消Windows系統磁盤容量警告
  當發布Windows XP操作系統的時候,微軟一直努力去做的就是想方設法讓其傻瓜化——增加了各種各樣的鈴聲和口哨聲以提示用戶使其免受各種麻煩困擾。其中一種便是磁盤容量不足提示警告(Low Disk Space Notificatio...查看完整版>>如何取消Windows系統磁盤容量警告
 
取消Windows XP系統磁盤容量警告
再大的硬盤也有填滿的一天,這時電腦就會提醒硬盤空間不足,這種警告有時候顯得太煩人。   如果是Win98系統,只要在磁盤圖標上單擊右鍵,就可以看到取消選項。不過在WinXP裏面,要修改系統注冊表來關閉此警告。運行...查看完整版>>取消Windows XP系統磁盤容量警告