procedure TForm1.AddTree(Path: string; ParentNode: TTreeNode);
var
  SearchRec : TSearchRec;
  IFound : integer;
  newnode : TTreeNode;
begin
  IFound := FindFirst(Path + '\*.*', faAnyFile, SearchRec);
  While IFound = 0 do
  begin
    if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
    begin
      if SearchRec.Attr = faDirectory then
      begin
        newnode := TreeView1.Items.AddChild(ParentNode, SearchRec.Name);
        AddTree(Path + '\' + SearchRec.Name, newnode);
      end
      else
        TreeView1.Items.AddChild(ParentNode, SearchRec.name);
    end;
    IFound := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

解决方案 »

  1.   

    反复使用Copy和Delete
    对几个路径字符串进行操作每次进行判断
      

  2.   

    我的目的不是递归一个目录下的所有文件,而是我有这样一组路径数据存放于Tlist中
    我想通过字符串操作,得到这样的一个树型结构显示!那位大虾能搞定???
      

  3.   

    Delphi 6 Sample页中有ShellTreeView控件。
      

  4.   

    type
      TForm1 = class(TForm)
        TreeView1: TTreeView;
        Edit1: TEdit;
        Button1: TButton;
        ListBox1: TListBox;
        procedure Panel1Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      tempList: TStringList;
      i: integer;
      Node: TTreeNode;
    begin
      tempList := TStringList.Create;
      try
        ExtractHttpFields(['\'],[],PChar(Edit1.Text),tempList);
        ListBox1.Items.Assign(tempList);
        Node := Treeview1.Items.Add(nil,templist.strings[0]);
        for i:=1 to tempList.Count -1 do
        begin
         Node := TreeView1.Items.AddChild(Node,tempList.strings[i]);
        end;
      finally
      tempList.Free;
      end;
    end;end.
      

  5.   

    object Edit1: TEdit
        Left = 280
        Top = 88
        Width = 241
        Height = 21
        TabOrder = 2
        Text = 'c:\test\1\2\1\1\.txt'
      end
      

  6.   

        一个文件路径可以这样实现,但是我需要的是一组这样的数据加入TreeView
    譬如:c:\test\1\1\1.txt
          c:\test\1\2\1\1\.txt
          c:\test2\1\2.txt      这样一组数据依次加入TreeView,而且节点不要有重复!
      

  7.   

    最方便的就是用delphi 6 中 samples面板中的 TShellTreeView 控件 
      

  8.   

    哈哈,我又会了,
    1。假设你的
    c:\test\1\1\1.txt
    c:\test\1\2\1\1\.txt
    c:\test2\1\2.txt
    放在ListBox1中
    2。假设你有Form1
    3。假设你有Button1和TreeView1
    procedure TForm1.Button1Click(Sender: TObject);
    var
      TempList: TStringList;
      i,index: integer;
      Node,temp: TTreeNode;
    begin
      for index:=0 to ListBox1.Items.Count-1 do
      begin
        tempList := TStringList.Create;
        try
          ExtractHttpFields(['\'],[],PChar(ListBox1.Items[index]),TempList);
          Node:=TreeView1.TopItem;
          for i:=0 to TempList.Count -1 do
          begin
            if i=0 then
            begin
              if Node=nil then
                Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
              else
                if Node.Text<>TempList.Strings[i] then Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
            end
            else
            begin
              temp:=Node.GetFirstChild;
              while temp<>nil do
              begin
                if temp.text=TempList.Strings[i] then
                begin
                  Node:=temp;
                  break;
                end;
                temp:=temp.GetNextSibling;
              end;
              if temp=nil then Node:=TreeView1.Items.AddChild(Node,TempList.Strings[i]);
            end;
          end;
        finally
          TempList.Free;
        end;
      end;
    end;
      

  9.   

    上面的有Bug,改了过来了
    procedure TForm1.Button1Click(Sender: TObject);
    var
      TempList: TStringList;
      i,index: integer;
      Node,temp: TTreeNode;
    begin
      for index:=0 to Memo1.Lines.Count-1 do
      begin
        tempList := TStringList.Create;
        try
          ExtractHttpFields(['\'],[],PChar(Memo1.Lines[index]),TempList);
          Node:=TreeView1.TopItem;
          for i:=0 to TempList.Count -1 do
          begin
            if i=0 then
            begin
              if Node=nil then
                Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
              else
              begin
                temp:=Node;
                while temp<>nil do
                begin
                  if temp.Text=TempList.Strings[i] then
                  begin
                    Node:=temp;
                    break;
                  end;
                  temp:=temp.getNextSibling;
                end;
                if temp=nil then Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
              end
            end
            else
            begin
              temp:=Node.GetFirstChild;
              while temp<>nil do
              begin
                if temp.Text=TempList.Strings[i] then
                begin
                  Node:=temp;
                  break;
                end;
                temp:=temp.GetNextSibling;
              end;
              if temp=nil then Node:=TreeView1.Items.AddChild(Node,TempList.Strings[i]);
            end;
          end;
        finally
          TempList.Free;
        end;
      end;
    end;
      

  10.   

    yandong_mars老大,你好厉害,我好崇拜你,我立马给分!!!!
    为了表示我对你的敬意,特意献上我的原创软件http://djvu.cn.gs,欢迎指点!!
      

  11.   

    谢谢了,我不能上那个网,把你的原创软件http://djvu.cn.gs邮过来让我看看吧
    [email protected]