//一个遍历所有硬盘的所有目录的实例源码: unit Unit1; interface uses 
Windows, Messages,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ImgList, ExtCtrls; type 
TForm1 = class(TForm)
    Button3: TButton;
    TreeView: TTreeView;
    Edit1: TEdit;procEDure Button3Click(Sender: TObject); 
private 
{ Private declarations } 
public 
procedure CreateDirectoryTree(RootDir, RootCaption: string); 
end;var 
Form1: TForm1; implementation{$R *.DFM}
procedure TForm1.CreateDirectoryTree(RootDir, RootCaption: string);
procedure AddSubDirToTree(RootNode: TTreeNode);
var 
SearchRec: TSearchRec; 
Path: string; 
Found: integer; 
begin
Path := PChar(RootNode.Data) + '\*.*';
//showmessage(Path);
Found := FindFirst(Path, faAnyFile, SearchRec);
while Found = 0 do 
begin
if (SearchRec.Attr = faDirectory) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then 
AddSubDirToTree(TreeView.Items.AddChildObject(RootNode, SearchRec.Name, 
PChar(PChar(RootNode.Data) + '\' + SearchRec.Name))); 
Found := FindNext(SearchRec); 
end; 
FindClose(SearchRec); 
end; 
begin
//TreeView.Items.Clear; 
AddSubDirToTree(TreeView.Items.AddObject(nil, RootCaption, PChar(RootDir))); 
end;procedure TForm1.Button3Click(Sender: TObject);
var 
i:integer; 
abc:Tstrings; 
s:string; 
begin 
abc:=TStringlist.Create;
for i:=0 to 23 do begin 
s := Chr(65+i)+':\'; 
// if GetDriveType(PChar(s))= DRIVE_cdrom then 
if directoryexists(s) then 
begin 
s:=copy(s,0,2) ; 
abc.Add(s); 
end; 
end; 
for i:=0 to abc.Count-1 do 
BEGIN 
S:=abc.strings[i]; 
CreateDirectoryTree(S, '['+s+'\]'); 
END 
end; 
end.
 

解决方案 »

  1.   

    点击运行不提示错误,确运行不下去,弹出个cpu的页面,我不知道是哪里错了,帮我找找吧,最好能改过来
      

  2.   

    还有个问题Button3Click中调用了CreateDirectoryTree(S, '['+s+'\]')过程,
    并未有给AddSubDirToTree(RootNode: TTreeNode)过程传参数,执行AddSubDirToTree时怎么会有值呢?
      

  3.   

    点击运行一直处于Running状态,点击停止出现错误,截图怎么传啊?错误图片弄不上来
      

  4.   

    你的程序我试过了,你说的一直处在running状态,是因为查找过程比较漫长所致,你在循环开始加一句Application.ProcessMessages;就可以跳出假死状态了,多等待一会,代码是可以运行完毕的
      

  5.   

    是这样加吗?
    while Found = 0 do
    begin
    Application.ProcessMessages;if (SearchRec.Attr = faDirectory) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
    高手请把#4楼这个问题也解释解释呗?
      

  6.   

    参数是通过
    TreeView.Items.AddObject(nil, RootCaption, PChar(RootDir))传进的