用 IOUtils 单元下的 TDirectory.GetFiles 获取文件列表太方便了;
下面的例子只是 TDirectory.GetFiles 的典型应用...
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} uses IOUtils, Types; {这是随意拿来的测试路径} const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0'; //获取指定目录下的所有文件 procedure TForm1.Button1Click(Sender: TObject); var dir: TDirectory; {这是个结构, 后面就不再声明了, 将直接使用} files: TStringDynArray; {TStringDynArray = array of string;} str: string; begin files := dir.GetFiles(path); Memo1.Clear; for str in files do Memo1.Lines.Add(str); end; //获取指定目录下的所有指定类型的文件 procedure TForm1.Button2Click(Sender: TObject); var files: TStringDynArray; str: string; begin files := TDirectory.GetFiles(path, '*.txt'); Memo1.Clear; for str in files do Memo1.Lines.Add(str); end; //获取指定目录及其嵌套目录下的所有指定类型的文件 procedure TForm1.Button3Click(Sender: TObject); var files: TStringDynArray; str: string; begin files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectories); Memo1.Clear; for str in files do Memo1.Lines.Add(str); end; end.