偶也贴个..在窗体上换一个ListBox1和Button1控件.就可以目录下所有文件路径显示在ListBox1列表里面.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TSearchFileThread=class(TThread)
private
MyFileName: string;
procedure SearchFile(DirName: string);
procedure DeelWithFile;
procedure SearchComplete;
public
MyDirName: string;
protected
procedure Execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TSearchFileThread.DeelWithFile;
begin
Form1.ListBox1.Items.Insert(0, MyFileName);
end;
procedure TSearchFileThread.SearchFile(DirName: String);
Var
Found: integer;
SearchRec: TSearchRec;
begin
Found := FindFirst(DirName + '*.*',faAnyFile,searchrec);
while Found = 0 do
begin
if ((SearchRec.Attr and faDirectory)<>0) then //directory
begin
if(SearchRec.Name <> '.')and(SearchRec.Name <> '..') then
SearchFile(DirName + SearchRec.Name + '/')
end
else //file
begin
MyFileName := DirName + SearchRec.Name;
Synchronize(DeelWithFile);
end;
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
procedure TSearchFileThread.SearchComplete;
begin
ShowMessage('搜索完毕,共找到' + inttostr(Form1.ListBox1.Items.Count) + '个文件');
end;
procedure TSearchFileThread.Execute;
begin
SearchFile(MyDirName);
Synchronize(SearchComplete);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
SearchFileThread: TSearchFileThread;
begin
SearchFileThread := TSearchFileThread.Create(true);
SearchFileThread.MyDirName := 'F:/'; //必须以 '/' 结尾
SearchFileThread.Resume;
end;
end.