在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.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值