未声明的标识符 soAllDirectories

Undeclared identifier soAllDirectories

我正在尝试使用 TDirectory.GetFiles 函数,但是当我添加 TSearchOptions 第三个参数以强制执行递归搜索时,编译器会引发错误,指出 soAllDirectories 尚未声明。

uses System.IOutils,
     System.Types;

procedure TfrmConversio.btnConversioClick(Sender: TObject);
var FilesPas: TStringDynArray;
begin
  FilesPas := TDirectory.GetFiles('C:\Project', '*.pas', soAllDirectories);
  ProgressBar1.Max := Length(FilesPas);
end;

我做错了什么?。我可以在 System.IOUtils.

中看到该常数

谢谢。

你需要写

TDirectory.GetFiles('C:\Project', '*.pas', TSearchOption.soAllDirectories);

原因是在TSearchOption类型的定义之前找到了编译指令{$SCOPEDENUMS ON}。这恰恰意味着您需要使用类型名称来限定枚举的常量。

来自文档:

The $SCOPEDENUMS directive enables or disables the use of scoped enumerations in Delphi code. More specifically, $SCOPEDENUMS affects only definitions of new enumerations, and only controls the addition of the enumeration's value symbols to the global scope.

In the {$SCOPEDENUMS ON} state, enumerations are scoped, and enum values are not added to the global scope. To specify a member of a scoped enum, you must include the type of the enum.