使用函数:
System.IOUtils.TDirectory.GetFiles
所有重载:
class function GetFiles(const Path: string): TStringDynArray; class function GetFiles(const Path: string; const Predicate: TFilterPredicate): TStringDynArray; class function GetFiles(const Path, SearchPattern: string): TStringDynArray; class function GetFiles(const Path, SearchPattern: string; const Predicate: TFilterPredicate): TStringDynArray; class function GetFiles(const Path, SearchPattern: string; const SearchOption: TSearchOption): TStringDynArray; overload; static; class function GetFiles(const Path, SearchPattern: string; const SearchOption: TSearchOption; const Predicate: TFilterPredicate): TStringDynArray; overload; static; class function GetFiles(const Path: string; const SearchOption: TSearchOption; const Predicate: TFilterPredicate): TStringDynArray; overload; static;
参数说明和TDirectory.GetDirectories一样,只是目录换成文件
异常处理: 目录无效或目录不存在
示例:获取指定目录下,文件名包含'a'且不为隐藏属性的所有txt文本文件。
代码部分:
var sFile: string; fp: TDirectory.TFilterPredicate; begin // fp := function(const Path: string; const SearchRec: TSearchRec): Boolean begin Result := (Pos('a', SearchRec.Name) > 0) and (SearchRec.Attr and faHidden <> faHidden); //文件名包含'a'且不为隐藏文件 end; try for sFile in TDirectory.GetFiles( 'H: mp1', //'H: mp1' '*.txt', //后缀为.txt TSearchOption.soAllDirectories,// 'H: mp1'目录下的所有文件 fp) do ListBox1.Items.Add(sFile); except on e: Exception do begin MessageDlg(e.Message, mtError, [mbok], 0); Exit; end; end; end;