组件:memo1
文件目录是:c:\wenjian
有3个文件 1.txt 2.txt 3.txt
如何使得memo1的内容为:
1.txt
2.txt
3.txt谢谢

解决方案 »

  1.   

    //win3.1下面-filelistbox filelistbox1.Directory:='c:\wenjian'
      

  2.   

    谢谢
    能不能实现过滤呢,比如只需要txt文件,而不需要别的格式
      

  3.   

    以前的两个函数,你看看吧...
    ---------------------------
    function GetFilenames(FilePath: string):TStrings;    //遍历目录下的文件名
    var
      FileRec :TSearchrec;
    begin
      if DirectoryExists(FilePath) then
      begin
        Result:=TStringList.Create;
        if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then
        repeat
          if (FileRec.Attr and faDirectory) = 0 then
              Result.Add(ExtractFileName(FilePath + FileRec.Name));
        until FindNext(FileRec) <> 0;
        FindClose(FileRec);
      end;
    end;function GetFilenamesEx(FilePath: string):TStrings;    //遍历目录及子目录下的文件名
    var
      FileRec :TSearchrec;
    begin
      if DirectoryExists(FilePath) then
      begin
        Result:=TStringList.Create;
        if FindFirst(FilePath+'*.*',faAnyfile,FileRec) = 0 then
        repeat
          if (FileRec.Attr and faDirectory) <> 0 then
          begin
            if (FileRec.Name<> '.') and (FileRec.Name  <> '..') then
               Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\'));
            end
          else Result.Add(ExtractFileName(FilePath + FileRec.Name));
        until FindNext(FileRec) <> 0;
        FindClose(FileRec);
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);  //测试部分
    begin
      ListBox1.Items := GetFilenames('c:\1\');
      ListBox2.Items := GetFilenamesEx('c:\1\');
    end;
      

  4.   

    考虑不周,现加了文件名后缀过滤...  -_-#
    --------------------------------------function GetFilenames(FilePath,ExtMask: String):TStrings;    //遍历目录
    var
      FileRec :TSearchrec;
    begin
      if DirectoryExists(FilePath) then
      begin
        Result:=TStringList.Create;
        if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then
        repeat
          if (FileRec.Attr and faDirectory) = 0 then
              Result.Add(ExtractFileName(FilePath + FileRec.Name));
        until FindNext(FileRec) <> 0;
        FindClose(FileRec);
      end;
    end;function GetFilenamesEx(FilePath,ExtMask :String):TStrings;    //遍历目录及子目录
    var
      FileRec :TSearchrec;
    begin
      if DirectoryExists(FilePath) then
      begin
        Result:=TStringList.Create;
        if FindFirst(FilePath+ExtMask,faAnyfile,FileRec) = 0 then
        repeat
          if (FileRec.Attr and faDirectory) <> 0 then
          begin
            if (FileRec.Name<> '.') and (FileRec.Name  <> '..') then
               Result.AddStrings(GetFilenames(FilePath + FileRec.Name + '\',ExtMask));
            end
          else Result.Add(ExtractFileName(FilePath + FileRec.Name));
        until FindNext(FileRec) <> 0;
        FindClose(FileRec);
      end;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ListBox1.Items := GetFilenames('c:\1\','*.txt');
      ListBox2.Items := GetFilenamesEx('c:\1\','*.txt');
    end;