我刚开始也没有注意到,但是使用后发现结果不对,源代码如下:
------------------------------
unit Unit1;interfaceuses
Windows, Messages, FileCtrl,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ImgList, ExtCtrls;type
TForm1 = class(TForm)
TreeView: TTreeView;
Button3: TButton;
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) + '\*.*';
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.
---------------------------------
我觉得关键在于CreateDirectoryTree过程中的 SearchRec.Attr = faDirectory 这句,因为在调试过程中faDirectory=16,而经常有SearchRec.Attr=48的情况出现,都没这么个情况的帮助文档,请大家帮忙看看,谢谢了
------------------------------
unit Unit1;interfaceuses
Windows, Messages, FileCtrl,SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ImgList, ExtCtrls;type
TForm1 = class(TForm)
TreeView: TTreeView;
Button3: TButton;
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) + '\*.*';
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.
---------------------------------
我觉得关键在于CreateDirectoryTree过程中的 SearchRec.Attr = faDirectory 这句,因为在调试过程中faDirectory=16,而经常有SearchRec.Attr=48的情况出现,都没这么个情况的帮助文档,请大家帮忙看看,谢谢了
(SearchRec.Attr and faDirectory) = faDirectory简单来说就是SearchRec.Attr的2进制01010101这样的东西,每个0和1都代表一个开关。
{
Stype:0 file
1 Dir
2 file&Dir
}
var
SearchRec:TSearchRec;
ReValue:Integer;
begin
if Apath = '' then
Exit;
if Copy(Apath,length(Apath),1)<>'\' then
Apath:=Apath+'\'; ReValue:=FindFirst(Apath+'*.*',faAnyFile,SearchRec); while ReValue = 0 do
begin
if (SearchRec.Attr and faAnyFile) = SearchRec.Attr then
case Stype of
0:begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
if (SearchRec.Attr and faDirectory)<>0 then
begin
if subDir then
Searchfile(Apath+SearchRec.Name,Stype,Exts,Aresult,subdir);
end
else
begin
if pos('*.*',Exts) > 0 then
begin
Aresult.Add(Apath+SearchRec.Name);
Result:=true;
end
else if pos(ExtractfileExt(SearchRec.Name),Exts) > 0 then
begin
Aresult.Add(Apath+SearchRec.Name);
Result:=true;
end;
end;
end;
1:begin
if ((SearchRec.Attr and faDirectory)<>0) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..')then
begin
Aresult.Add(Apath+SearchRec.Name);
Result:=true;
if subDir then
Searchfile(Apath+SearchRec.Name,Stype,Exts,Aresult,subdir);
end;
end;
2:begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..')then
begin
Aresult.Add(Apath+SearchRec.Name);
Result:=true;
if (SearchRec.Attr and faDirectory)<>0 then
if subDir then
Searchfile(Apath+SearchRec.Name,Stype,Exts,Aresult,subdir);
end;
end;
end;
ReValue:=FindNext(SearchRec);
end;
FindClose(SearchRec);
end;