哈哈,这个问题我来答,昨天我刚编了一个同样的程序,现在给你代码,他还是采用了递归方法
千万不要忘了给我分数呀!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, ImgList;
type
TForm1 = class(TForm)
Button1: TButton;
TreeView1: TTreeView;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: boolean);
var
SearchRec: TSearchRec;
ItemTemp: TTreeNode;
begin
TreeView1.Items.BeginUpdate;
if Directory[length(Directory)] <> '/' then Directory := Directory + '/';
if FindFirst(Directory + '*.*',faDirectory,SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then
begin
if (SearchRec.Attr and faDirectory > 0) then
begin
Item := Tree.Items.AddChild(Item,SearchRec.Name);
ItemTemp := Item.Parent;
GetDirectories(Tree,Directory + SearchRec.Name,Item,IncludeFiles);
Item := ItemTemp;
end;
end
else if IncludeFiles then
if SearchRec.Name[1] <> '.' then
begin
Tree.Items.AddChild(Item,SearchRec.Name);
end;
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
TreeView1.Items.EndUpdate;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
GetDirectories(TreeView1,edit1.Text,nil,True);
end;
end.
上面的程序中edit1中只要输入查找的路径就行了;比如输入"d:/"便可查你要求的硬盘目录
输入其他子树也可以