[c:\root\][c:\root\aaa\][c:\root\aaa\a1\][c:\root\aaa\a2\][c:\root\aaa\a1\a11][c:\root\aaa\a1\a12][c:\root\bbb\][c:\root\bbb\b1\][c:\root\ccc\]root----aaa
     |    |----a1
     |    |    |---a11
     |    |    |---a12
     |    |    
     |    |----a2
     |
     |--bbb
     |    |---b1
     |
     |--ccc
如何将上面格式的INI文件转成下面的TREEVIEW,
实际的INI文件中的目录数是不固定的,但都是一个根目录下的。

解决方案 »

  1.   

    先创建一级TreeNode(Root),然后找到所有二级目录,逐个创建,完成二级目录后,
    先找出第一个二级目录的所有直接下级,逐个创建,然后下一个。。依次类推
    直到最后一级
      

  2.   

    就是问你怎么写,我写的
    for i:=0 to treeview.items.count-1 do
    begin
        node:=treeview.item[i];
        插入node下一级过程;
    end;由于插入时真实的COUNT是增加的,所以不能完全遍历上一级节点,请大侠帮忙!
      

  3.   

    先谢过tonylk(tony) 了。
      

  4.   

    procedure TFormMenuEdit.OpenFile(FileName:String);
    //open an file, load it into treeview.
    //2003.02.04
    var
      Ini:TIniFile;
      SectionCount,ItemCount,i,j:integer;
      TreeNode:TTreeNode;
    begin
      Ini:=TIniFile.Create(ExtractFilePath(Application.ExeName)+DescriptPath+FileName);
      try
        TreeViewMenu.Items.BeginUpdate;
        TreeViewMenu.Items.Clear;
        TreeNode:=nil;
        with TreeViewMenu.Items.Add(TreeNode,Ini.ReadString('General','Name',AFileName)) do begin
          ImageIndex:=0;
          SelectedIndex:=0;
        end;
        SectionCount:=Ini.ReadInteger('General','SectionCount',0);
        for i:=1 to SectionCount do begin
          TreeNode:=TreeViewMenu.Items.GetFirstNode;
          with TreeViewMenu.Items.AddChild(TreeNode,Ini.ReadString('Section'+IntToStr(i),'Name','')) do begin
            ImageIndex:=1;
            SelectedIndex:=1;
          end;
          ItemCount:=Ini.ReadInteger('Section'+IntToStr(i),'ItemCount',0);
          TreeNode:=TreeNode.GetLastChild;
          for j:=1 to ItemCount do begin
            with TreeViewMenu.Items.AddChild(TreeNode,Ini.ReadString('Section'+IntToStr(i),'Item'+IntToStr(j),'')) do begin
              ImageIndex:=2;
              SelectedIndex:=2;
            end;
          end;
        end;
        if TreeViewMenu.Items.GetFirstNode<>nil then
          TreeViewMenu.Items.GetFirstNode.Expand(false); 
      finally
        TreeViewMenu.Items.EndUpdate;
        Ini.Free;
      end;
    end;我这个程序里使用的ini文件中用
    [General]
    SectionCount=xx
    来定义有多少个section,
    每个Section下用
    [SectionX]
    ItemCount=xx
    来定义该section下有多少个item,你的程序中可以使用ini.ReadSections来得到section列表,ini.ReadSection来得到某个section下有多少个item,照这个把程序稍许改一下就可以了。
      

  5.   

    之前没有仔细看你的要求,回答错了,抱歉你的问题是没法遍历某一级节点吗?只要取得你要使用的节点,可以通过TreeNode.getNextSibling,来访问同一层次内的其它节点。一般我觉得应该这么干:用TStringList.DelimitedText方法,将每一级目录名给分割开,然后利用循环遍历这个目录层次,并同时从treeview的定端节点开始检索,遇到当前节点的子节点中(遍历子节点)没有当前目录层次内的目录名,就创建之,反之,将当前节点设置为这个找到的节点,然后进入下一次循环。不知道说明白了没有
      

  6.   

    首先感谢大家,我昨天晚上已经自己写好了一个递归算法。
    现在公布出来,希望对大家有个帮助!
    其中lshi
    function addtotree(rootnode: TTreeNode): boolean;
    var num, i: integer;
      n: TStringlist;
      nodes: array of TTreeNode;
    begin
      n := TStringlist.Create;
      n.Clear;  num := strsubcount(rootnode.Text, '\');  for i := 0 to l.Count - 1 do
      begin
        if (strsubcount(l.Strings[i], '\') = num + 1) and (pos(rootnode.Text, l.strings[i]) > 0) then
          n.Add(l.Strings[i]);
      end;  setlength(nodes, n.Count);  for i := 0 to n.Count - 1 do
      begin
        form1.treeview1.Items.BeginUpdate;
        nodes[i] := form1.treeview1.Items.AddChildObject(rootnode, n.Strings[i], nil);
        form1.treeview1.Items.EndUpdate;
      end;  for i := 0 to n.count - 1 do
      begin
        addtotree(nodes[i]);
      end;  n.Free;  result := true;
    end;
      

  7.   

    其中l是tstringlist类的全局变量,保存所有的section.
    strsubcounts是统计子串的个数的函数。
      

  8.   

    按照我的理论写的。。procedure TForm1.CreateTreeView(AFileName:String);
    var
      PathList, Path:TStringList;
      Node, NodeChild:TTreeNode;
      I,J:Integer;
    begin
      PathList:=TStringList.Create();
      Path:=TStringList.Create();
      try
        PathList.LoadFromFile(AFileName);
        Path.Delimiter:='\';
        Node:=nil;
        for I:=0 to PathList.Count-1 do begin
          Path.DelimitedText:=PathList.Strings[I];
          Node:=nil;
          for J:=0 to Path.Count-1 do begin
            if Node=nil then begin
              NodeChild:=TreeView1.Items.GetFirstNode();
            end
            else begin
              NodeChild:=Node.getFirstChild();
            end;
            while NodeChild<>nil do begin
              if NodeChild.Text=Path.Strings[J] then begin
                Node:=NodeChild;
                break;
              end;
              NodeChild:=NodeChild.GetNext();
            end;
            if NodeChild=nil then begin
              Node:=TreeView1.Items.AddChild(Node,Path.Strings[J]);
              //{ enable these codes to see the effect
              if Assigned(Node.Parent) then begin
                Node.Parent.Expand(false);
              end;
              Application.ProcessMessages;
              Sleep(500);
              //}
            end;
          end;//for J
        end;//for I
      finally
        PathList.Free;
        Path.Free;
      end;
    end;
      

  9.   

    改进一下,增加两行:    for I:=0 to PathList.Count-1 do begin
          Path.DelimitedText:=PathList.Strings[I];
    //**
          if Length(Path.Strings[Path.Count-1])=0 then begin
            Path.Delete(Path.Count-1);
          end;
    //**
          Node:=nil;
          for J:=0 to Path.Count-1 do begin