欢迎光临
我们一直在努力

在DELPHI中获得磁盘容量-.NET教程,评论及其它

建站超值云服务器,限时71元/月

使用如下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)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在DELPHI中获得磁盘容量-.NET教程,评论及其它
分享到: 更多 (0)