想将选择的文件夹内的文件名放到TListBox列表中,当文件夹内文件数量很多时会有以下几个问题:1.使用Opendialog控件选择文件夹里的文件时,若太多,比如我选择的有7万个左右的文件,一是打开文件夹本身就慢,再按全选也是有点卡的,这些都能忍,可问题时,最后显示出来的文件数量都只有1261条。我之前以为是Memo的问题,后来调代码发现 Opendialog1.Files.Count本身的数量就只有1261条了。这个有什么办法解决吗?2.使用遍历函数用TSearchRec去遍历文件夹的方法去取文件,如果数量少有时候可以,但有时候如果文件夹内有太多文件就要遍历很久很久就好像卡死了一样,然后显示出找到0个文件。

解决方案 »

  1.   


    同一个程序 选同样的文件夹,有时候卡死,有时候显示成0个,遍历方法也是网上找的function MakeFileList(Path,FileExt:string):TStringList ;
    var
      sch:TSearchrec;
    begin
      Result:=TStringlist.Create;
      if rightStr(trim(Path), 1) <> '\' then
        Path := trim(Path) + '\'
      else
        Path := trim(Path);
      if not DirectoryExists(Path) then
      begin
        Result.Clear;
        exit;
      end;
      if FindFirst(Path + '*', faAnyfile, sch) = 0 then
      begin
        repeat
          Application.ProcessMessages;
          if ((sch.Name = '.') or (sch.Name = '..')) then
            Continue;
          if DirectoryExists(Path+sch.Name) then
          begin
            Result.AddStrings(MakeFileList(Path+sch.Name,FileExt));
          end
          else
          begin
            if (UpperCase(extractfileext(Path+sch.Name)) = UpperCase(FileExt)) or (FileExt='.*') then
              Result.Add(Path+sch.Name);
          end;
        until FindNext(sch) <> 0;
        SysUtils.FindClose(sch);
      end;
    end;
      

  2.   

    我试了下你的代码, 正常啊。
     lst1.Items:=MakeFileList('C:\Windows\System32','.dll');
    很快就能出来结果了。
      

  3.   

    试了下Opendialog读到的文件数量不止1261个,还有第二种方法可以遍历的是不是MakeFileList第二个参数少写了个'.',比如传'.dll'写成了’dll',呵呵,我写时候漏了就没查了出来。
      

  4.   


    你把文件夹内文件数量变成6万以上再试试用opendialog看有不能全部读到啊,如果数量少的话是可能读全的,但数量太大就会这样子。
      

  5.   


    //Delphi 2010以上版本可用   我遍历了1W+, 没问题
    uses IOUtils, Types;procedure TForm11.btn1Click(Sender: TObject);
    var
      files: TStringDynArray; {TStringDynArray = array of string;}
      str: string;
    begin
      files := TDirectory.GetFiles('f:\test');  Mmo1.Visible := False;  //隐藏可减少界面刷新
      Mmo1.Clear;
      for str in files do Mmo1.Lines.Add(str);
      Mmo1.Visible := True;  //文件数量
      Caption := IntToStr(mmo1.Lines.Count);