如何打开所有 *.txt 到列表框

How to open all *.txt to a listbox

我需要在ListBox(一个有很多*.txt文件的文件夹)中打开这些文件的内容。我想知道一种将该文件夹中的所有文本文件加载到 ListBox 中的方法。我需要通过单击或程序打开列表框中的所有 .txt 文件。

你可以这样做:

var
  path: string;
  SR: TSearchRec;
  tempFile: TextFile;
  line: string;
begin
  path:= 'C:\ insert path to your folder\';
  if FindFirst(path + '*.txt', faAnyFile, SR) = 0 then
  begin
    repeat
      if (SR.Attr <> faDirectory) then
      begin
        AssignFile(tempFile, path + SR.Name);
        Reset(tempFile);
        while not Eof(tempFile) do
        begin
          Readln(tempFile, line);
          ListBox1.Items.Add(line);
        end;
      end;
    until FindNext(SR) <> 0;
    FindClose(SR);
  end;
end;

放在点击按钮上或包装到程序中。