如题。

解决方案 »

  1.   

    遍历文件夹中所有文件,求每个文件大小,最后求总和代码自己找找吧,至于求单个文件大小,应该有相关API
      

  2.   

    function GetDirectorySize(const ADirectory: string): Integer;
    var
     Dir: TSearchRec;
     Ret: integer;
     Path: string;
    begin
     Result := 0;
     Path := ExtractFilePath(ADirectory);
     Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
     if Ret<>0  then
      begin
       ShowMessage('路径输入错误');
       exit;
      end; try
      while ret=0 do
       begin
        inc(Result, Dir.Size);
        if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
        Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
        Ret := Sysutils.FindNext(Dir);
       end;
     finally
     Sysutils.FindClose(Dir);
     end;
    end;
      

  3.   

    我给你一个实例:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, FileCtrl, ComCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        PageControl1: TPageControl;
        TabSheet1: TTabSheet;
        Panel1: TPanel;
        ListView1: TListView;
        Panel2: TPanel;
        Button2: TButton;
        Edit1: TEdit;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure IniListView();
        procedure ListView1Click(Sender: TObject);
        procedure FormShow(Sender: TObject);
        procedure ListView1DblClick(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      function GetDirectoryName(Dir:String):String;  //&Eacute;è&Ouml;&Atilde;DIR
      procedure FindFiles(APath:String);    //&sup2;é&Otilde;&Ograve;&Ouml;÷&ordm;&macr;&Ecirc;&yacute;  end;var
      Form1: TForm1;
      Dir1:string;
      pathpos:integer;
      selectpath:string;
    implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      //winexec('rundll32.exe shell32.dll,Control_RunDLL Intl.cpl,,0',9);
      close;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
       SelectDirectory('Select Directory', '', Dir1);
       if Dir1<>'' then
         begin
           edit1.Text:=Dir1;
           ListView1.Items.Clear;
           FindFiles(Dir1);
         end;
    end;procedure TForm1.IniListView();
    var
      NewColumn: TListColumn;
    begin
      with ListView1 do
      begin
        Parent := panel1;
        Align := alClient;
        OnClick := ListView1Click;  // <- &frac14;&Oacute;&Eacute;&Iuml;&Otilde;&acirc;&frac34;&auml;    ViewStyle := vsReport;    NewColumn := Columns.Add;
        NewColumn.Caption := '&Euml;&Oslash;&sup2;&Auml;&Atilde;&ucirc;&sup3;&AElig;';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '&Icirc;&Auml;&frac14;&thorn;&Agrave;à&ETH;&Iacute;';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '&Acirc;·&frac34;&para;';
        NewColumn.Width:=200;
        NewColumn := Columns.Add;
        NewColumn.Caption := '&Icirc;&Auml;&frac14;&thorn;&acute;ó&ETH;&iexcl;';
        NewColumn.Width:=100;
        NewColumn := Columns.Add;
        NewColumn.Caption := '&cedil;ü&ETH;&Acirc;&Ecirc;±&frac14;&auml;';
        NewColumn.Width:=100;  end;
    end;function Tform1.GetDirectoryName(Dir:String):String;  //&Eacute;è&Ouml;&Atilde;DIR
    begin
      if Dir[LengTh(Dir)]<>'\' then
        Result:=Dir+'\'
      else
        Result:=Dir;
    end;procedure Tform1.FindFiles(APath:String);    //&sup2;é&Otilde;&Ograve;&Ouml;÷&ordm;&macr;&Ecirc;&yacute;
    var
      FSearchRec,DsearchRec:TSearchRec;
      FindResult:integer;
      it:tlistitem;
      fhandle:thandle;
      hip:pointer;
      filesize:longint;
      /////////
      tempstr:string;
    function IsDirNotation(ADirName:String):Boolean;
    begin
      Result:=(ADirName='.') or(ADirName='..')or(ADirName='') ;
    End;begin
     Apath:=GetDirectoryName(APath);
     FIndResult:=FindFirst(APath+'*.*',faAnyFile+faHidden+faSysFile+FaReadonly,FsearchRec);
     Try
     while FindResult=0 do
     begin
     it:=listview1.Items.Add;
     it.Caption:=extractfilename(APath+FSearchRec.Name);    //°&Ntilde;&sup2;é&Otilde;&Ograve;&micro;&frac12;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;&Aacute;&ETH;&sup3;&ouml;&Agrave;&acute;
     it.SubItems.Add(extractfileext(APath+FSearchRec.Name));//&Aacute;&ETH;&sup3;&ouml;&Icirc;&Auml;&frac14;&thorn;&micro;&Auml;&Icirc;&Auml;&frac14;&thorn;&Agrave;à&ETH;&Iacute;
     it.SubItems.Add(APath+FSearchRec.Name); fhandle:=createfile(pchar(APath+FSearchRec.Name),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
     new(hip);
     filesize:=GetFileSize(fhandle,hip);
     dispose(hip);
     it.SubItems.Add(FormatFloat('#,000',filesize)+'   ×&Ouml;&frac12;&Uacute;');
     //&raquo;&ntilde;&micro;&Atilde;&Euml;ù&Ntilde;&iexcl;&Acirc;·&frac34;&para;&Ograve;&Ocirc;&Iuml;&Acirc;&Icirc;&Auml;&frac14;&thorn;&Acirc;·&frac34;&para;
     //tempint:=pos(nowdir,copy(apath,pathpos,length(apath)-pathpos+1));
     //tempstr:=copy(apath,tempint+pathpos,length(apath)-tempint-pathpos);
     tempstr:=copy(apath,pathpos,length(apath)-pathpos+1);
     it.SubItems.Add(selectpath+tempstr+it.Caption);
     closehandle(fhandle);
     FindResult:=FindNext(FSearchRec);
     end;
     FindReSult:=FindFirst(APath+'*.*',faDirectory,DSearchRec);
     while FindResult=0 do
     begin
     if (DSearchRec.Attr{ and faDirectory)}=faDirectory) and not ISDirNotation(DSEarchRec.Name) then
     FindFiles(APath+DsearchRec.Name);
     FindResult:=FindNext(DsearchREc);
     end;
     Finally
      FindClose(FSearchRec);
      findclose(DsearchREc);
     end;
    end;procedure TForm1.ListView1Click(Sender: TObject);
    begin
      //hjfgh
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
           IniListView;
           edit1.Text:=extractfilepath(Application.ExeName);
           FindFiles(extractfilepath(Application.ExeName));
    end;procedure TForm1.ListView1DblClick(Sender: TObject);
    var
      i:integer;
    begin
          if TListView(Sender).ItemIndex>=0 then  // °&Ntilde;ListView1&cedil;&Auml;&Icirc;&ordf;TListView(Sender)
            begin
              i:=TListView(Sender).ItemFocused.Index;
              showmessage(ListView1.Items[i].SubItems[1])
            end
    end;end.
    结贴吧~