使用函数:
System.IOUtils.TDirectory.GetFileSystemEntries
所有重载:
class function GetFileSystemEntries(const Path: string): TStringDynArray; class function GetFileSystemEntries(const Path: string; const Predicate: TFilterPredicate): TStringDynArray; class function GetFileSystemEntries(const Path, SearchPattern: string): TStringDynArray; overload; static; class function GetFileSystemEntries(const Path, SearchPattern: string; const Predicate: TFilterPredicate): TStringDynArray; overload; static; class function GetFileSystemEntries(const Path: string; const SearchOption: TSearchOption; const Predicate: TFilterPredicate): TStringDynArray; overload; static;
参数说明和TDirectory.GetDirectories一样,只是目录和文件
异常处理: 目录无效或目录不存在
示例:
获取指定目录下所有'a'开头的文件和文件夹
代码部分:
var sPathOrFile: string; fp: TDirectory.TFilterPredicate; begin fp := function(const Path: string; const SearchRec: TSearchRec): Boolean begin Result := TPath.MatchesPattern(SearchRec.Name, 'a*', False); //匹配a开头的文件夹和文件 end; try ; for sPathOrFile in TDirectory.GetFileSystemEntries('H: mp', TSearchOption(1), fp) do ListBox1.Items.Add(sPathOrFile); //获取'H:'目录下的所有文件和文件夹(包含子目录递归) except on e: Exception do begin MessageDlg(e.ClassName + ' : ' + e.Message, mtError, [mbok], 0); Exit; end; end; end;