也许是这样的?
unit Unit2;
interface
uses
SysUtils, Classes;
type
TFileSearcher = class(TThread)
private
{ Private declarations }
FPath, FMask: string;
FIncludeSubDir: boolean;
FItems: TStrings;
function FindFiles: integer;
procedure UpdateTheMemo;
public
constructor Create(CreateSuspended: boolean; const Path, Mask: string; IncludeSubDir: boolean);
protected
procedure Execute; override;
end;
implementation
uses Unit1;
{ TFileSearcher }
constructor TFileSearcher.Create(CreateSuspended: boolean; const Path, Mask: string;
IncludeSubDir: boolean);
begin
inherited Create(CreateSuspended);
FPath := Path;
FMask := Mask;
FIncludeSubDir := IncludeSubDir;
end;
procedure TFileSearcher.Execute;
begin
FItems := TStringList.Create;
try
FindFiles;
Synchronize(UpdateTheMemo);
finally
FItems.Free;
end;
end;
procedure TFileSearcher.UpdateTheMemo;
begin
Form1.Memo2.Lines.Assign(FItems);
end;
function TFileSearcher.FindFiles: integer;
var
FindResult: integer;
SearchRec: TSearchRec;
ThisPath: string;
begin
ThisPath := FPath;
Result := 0;
FindResult := FindFirst(FPath + FMask, faAnyFile - faDirectory, SearchRec);
while FindResult = 0 do
begin
FItems.Add(FPath + SearchRec.Name);
Result := Result + 1;
FindResult := FindNext(SearchRec);
end;
FindClose(SearchRec);
if not FIncludeSubDir then
Exit;
FindResult := FindFirst(IncludeTrailingBackslash(ThisPath) + '*.*', faDirectory, SearchRec);
while FindResult = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
FPath := IncludeTrailingBackslash(ThisPath + SearchRec.Name);
FIncludeSubDir := true;
Result := Result + FindFiles();
end;
FindResult := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
end.
如果您希望将项目逐个添加到 VCL 控件中,您将失去线程的一些好处,但可以肯定的是,它可以做到:
unit Unit2;
interface
uses
SysUtils, Classes;
type
TFileSearcher = class(TThread)
private
{ Private declarations }
FPath, FMask: string;
FIncludeSubDir: boolean;
FItemToAdd: string;
function FindFiles: integer;
procedure UpdateTheMemo;
public
constructor Create(CreateSuspended: boolean; const Path, Mask: string; IncludeSubDir: boolean);
protected
procedure Execute; override;
end;
implementation
uses Unit1;
{ TFileSearcher }
constructor TFileSearcher.Create(CreateSuspended: boolean; const Path, Mask: string;
IncludeSubDir: boolean);
begin
inherited Create(CreateSuspended);
FPath := Path;
FMask := Mask;
FIncludeSubDir := IncludeSubDir;
end;
procedure TFileSearcher.Execute;
begin
FindFiles;
end;
procedure TFileSearcher.UpdateTheMemo;
begin
Form1.Memo2.Lines.Add(FItemToAdd);
end;
function TFileSearcher.FindFiles: integer;
var
FindResult: integer;
SearchRec: TSearchRec;
ThisPath: string;
begin
ThisPath := FPath;
Result := 0;
FindResult := FindFirst(FPath + FMask, faAnyFile and not faDirectory, SearchRec);
while FindResult = 0 do
begin
FItemToAdd := FPath + SearchRec.Name;
Synchronize(UpdateTheMemo);
Result := Result + 1;
FindResult := FindNext(SearchRec);
end;
FindClose(SearchRec);
if not FIncludeSubDir then
Exit;
FindResult := FindFirst(IncludeTrailingBackslash(ThisPath) + '*.*', faDirectory, SearchRec);
while FindResult = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
FPath := IncludeTrailingBackslash(ThisPath + SearchRec.Name);
FIncludeSubDir := true;
Result := Result + FindFiles();
end;
FindResult := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
end.