如何遍历文件夹,获得文件夹文件名,并将这些文件名放到组合框下拉列表里?

解决方案 »

  1.   


    var
      SearchRec: TSearchRec;
      Path: string;
      Attr: Integer;
      Found: Integer;
    begin
      Path := ExtractFilePath(ParamStr(0)) + '*.ini';//设定要显示的路径
      Attr := faAnyFile;
      Found := FindFirst(Path, Attr, SearchRec);//查找文件
      while Found = 0 do
      begin
        Cmbox1.Items.Add(Copy(SearchRec.Name, 1, length(SearchRec.Name) - 4));
        Found := FindNext(SearchRec);
      end;
      FindClose(SearchRec);//停止查找
      CmBox1.ItemIndex := 0;
    end;
      

  2.   

    上边的是ini文件的,把*.ini换成你想要的文件类型即可
      

  3.   


      //=====================================================================   
      //   函数名称:   FindAllFiles   
      //   功能描述:   找指定目录下的所有文件,包括子目录   
      //   参         数:     APath         :   路径名称   
      //                         APropty     :   属性名称(*.*   |   *.txt)   
      //                         AFiles       :   文件列表   
      //                         IsAddPath:   是否增加路径   
      //   作者:           
      //   时间:           
      //   返   回   值:   
      //   说         明:   
      //=====================================================================   
      procedure   FindAllFiles(const   APath:   string;   AFiles:   TStrings;   
          const   APropty:   String   =   '*.*';   IsAddPath:   Boolean   =   False);   
      var   
          FS:   TSearchRec;   
          FPath:   String;   
          AddPath:   string;   
      begin   
          FPath   :=   IncludeTrailingPathDelimiter(APath);   
          AddPath   :=   IfThen(IsAddPath,   FPath,   '');   
          if   FindFirst(FPath   +   APropty,   faAnyFile,   FS)   =   0   then   
          begin   
              repeat   
              if   (FS.Name   <>   '.')   and   (FS.Name   <>   '..')   then   
                  if   ((FS.Attr   and   faDirectory)   =   faDirectory)   then   
                      FindAllFiles(FPath   +   FS.Name,   AFiles,   APropty,   IsAddPath)   
                  else   
                      AFiles.Add(AddPath   +   FS.Name);   
              until   FindNext(FS)   <>   0;   
              SysUtils.FindClose(FS);   
          end;   
      end;调用方法FindAllFiles('c:\tmp', ComboBox1.Items, '*.*', true);