ListView如何才能显示某一文件夹下的所有文件,例如:C:\File\文件夹下的所有文件,这一文件夹下是没有子目录的,只有文件,我大概知道应该用文件的遍历,但实现起来确实不是那么容易了,还忘大家多多指教,谢谢!首先声明不能用ShellListView,用的是最基本的ListView。

解决方案 »

  1.   

    用下面两个函数能实现:
    FindFirst
    FindNext
    查查delphi自带的帮助就行了!
      

  2.   

    同意: bizzard008(暴雪) 一说
      

  3.   

    Procedure  TForm1.display(sDirectory:string);
    var        ListItem :TlistItem;
               sr:TSearchRec;
               s:string;
    begin
               if   (FindFirst(sDirectory+'\*.*',0,sr) =0)
               then  showmessage(sDirectory);
                     if   (sr.Attr <>0)//(sr.Attr =32)
                     then begin
                            ListItem :=ListView1.Items.Add ;
                              ListItem.Caption :=sr.Name;
                              ListItem.ImageIndex :=0;
                              ListItem.SubItems.Add(Inttostr(sr.Attr ));
                              ListItem.SubItems.Add(inttostr(sr.Size ));
                              DateTimeTostring(s,'',FileDateToDateTime(sr.Time ));
                              ListItem.SubItems.Add(s);
                          end;
               //end;           while   (FindNext(sr)=0)  do
                begin
                       if   (sr.Attr <>0)//(sr.attr =32)
                       then begin
                                    ListItem :=ListView1.Items.Add ;
                                    ListItem.Caption :=sr.Name;
                                    ListItem.ImageIndex :=0;
                                    ListItem.SubItems.Add(inttostr(sr.Attr ));
                                    ListItem.SubItems.Add(inttostr(sr.Size ));
                                    DateTimetoString(s,'',FileDateToDateTIme(sr.Time ));
                                    ListItem.SubItems.Add(s);
                            end;
                end;
    end;
    呵呵,这是我 写的一段代码,能够实现你的功能
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);
      procedure ReadFileName(var MyFileList : TStringList; const AnyFile:String); 
      var
        Found:integer; 
        SearchResult:TSearchRec;
        ListItem: TListItem;
      begin
        if FileGetAttr(AnyFile)<>faDirectory then begin 
          MyFileList.Add(AnyFile);
          memo1.Lines.Add(anyfile);
          with ListView1 do
            begin
            ListItem := Items.Add;
            ListItem.Caption := anyfile;
            ListItem.SubItems.Add(anyfile);
            end;
          Exit;
        end; 
        Found:=FindFirst(AnyFile+'\*.*',faAnyFile,SearchResult);
        while Found=0 do begin
          if (SearchResult.Name<>'.')and(SearchResult.Name<>'..') then ReadFileName(MyFileList,AnyFile+'\'+SearchResult.Name);
          Found:=FindNext(SearchResult); 
        end;
        if (SearchResult.Name<>'.')and(SearchResult.Name<>'..') then FindClose(SearchResult);
      end;
    var
      MyFileList : TStringList;
      NewColumn: TListColumn;
    begin
      MyFileList := TStringList.Create;
      with listview1 do
      begin
        NewColumn := Columns.Add;
        NewColumn.Caption := 'files';
        ViewStyle := vsReport;
      end;
      ReadFileName(MyFileList,'c:\file');
    end;