8

私はファイルシステムを扱っており、ファイルパスをプロパティとして持つファイルオブジェクトの List<> を持っています。基本的には.NETでツリービューを作成する必要がありますが、次のようなリストからツリー構造を作成する必要があるため、これを行うための最良の方法を考えるのに苦労しています:

C:/WINDOWS/Temp/ErrorLog.txt
C:/Program Files/FileZilla/GPL.html
C:/Documents and Settings/Administrator/ntuser.dat.LOG

等....

リストはまったく構造化されておらず、現在のオブジェクト構造を変更することはできません。

私はC#で働いています。

貢献してくれたすべての人に感謝します

4

5 に答える 5

16

弦に固執したい場合は、このようなものが機能します...

TreeNode root = new TreeNode();
TreeNode node = root;
treeView1.Nodes.Add(root);

 foreach (string filePath in myList) // myList is your list of paths
 {
    node = root;
    foreach (string pathBits in filePath.Split('/'))
    {
      node = AddNode(node, pathBits);
    }
 }


private TreeNode AddNode(TreeNode node, string key)
{
    if (node.Nodes.ContainsKey(key))
    {
        return node.Nodes[key];
    }
    else
    {
        return node.Nodes.Add(key, key);
    }
}
于 2009-03-23T16:33:55.103 に答える
2
    private void Form1_Load(object sender, EventArgs e)
    {
        var paths = new List<string>
                        {
                            @"C:\WINDOWS\AppPatch\MUI\040C",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
                            @"C:\WINDOWS\addins",
                            @"C:\WINDOWS\AppPatch",
                            @"C:\WINDOWS\AppPatch\MUI",
                            @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
                        };
        treeView1.PathSeparator = @"\";
        PopulateTreeView(treeView1, paths, '\\');
    }

    private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
    {
        TreeNode lastNode = null;
        string subPathAgg;
        foreach (string path in paths)
        {
            subPathAgg = string.Empty;
            foreach (string subPath in path.Split(pathSeparator))
            {
                subPathAgg += subPath + pathSeparator;
                TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
                if (nodes.Length == 0)
                    if (lastNode == null)
                        lastNode = treeView.Nodes.Add(subPathAgg, subPath);
                    else
                        lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
                else
                    lastNode = nodes[0];
            }
        }
    }

代替テキスト

于 2009-11-20T05:01:22.067 に答える
2

再帰を試してください。

private void AddFiles()
{
  // Iterate your list with FileInfos here
  foreach( var fileInfo in new Collection<FileInfo>() )
  {
    GetOrCreateTreeNode( fileInfo.Directory ).Nodes.Add( new TreeNode( fileInfo.Name ) );
  }
}

private TreeNode GetOrCreateTreeNode( DirectoryInfo directory )
{
  if( directory.Parent == null )
  {
    // Access your TreeView control here:
    var rootNode = <TreeView>.Nodes[directory.Name];
    if( rootNode == null )
    {
      rootNode = new TreeNode(directory.Name);
      // Access your TreeView control here:
      <TreeView>.Nodes.Add( rootNode );
    }
    return rootNode;
  }

  var parent = GetOrCreateTreeNode( directory.Parent );
  var node = parent.Nodes[directory.Name];
  if( node == null )
  {
    node = new DirectoryNode( directory );
    parent.Nodes.Add( node );
  }
  return node;
}

このコードはあなたにアイデアを与えるだけです - ここに投稿する前にテストしなかったことを認めなければなりません.

于 2009-03-23T16:32:41.923 に答える
0

EHosca's piece worked for me perfectly, with one change - I had to set lastnode to nothing after the foreach path in paths area.

This is eHosca's code above, ported to VB.

Private Sub PopulateTreeView(tv As TreeView, paths As List(Of String), pathSeparator As Char)
    Dim lastnode As TreeNode = Nothing
    Dim subPathAgg As String
    For Each path In paths
        subPathAgg = String.Empty
        lastnode = Nothing
        For Each subPath In path.Split(pathSeparator)
            subPathAgg += subPath + pathSeparator
            Dim nodes() As TreeNode = tv.Nodes.Find(subPathAgg, True)
            If nodes.Length = 0 Then
                If IsNothing(lastnode) Then
                    lastnode = tv.Nodes.Add(subPathAgg, subPath)
                Else
                    lastnode = lastnode.Nodes.Add(subPathAgg, subPath)
                End If
            Else
                lastnode = nodes(0)
            End If
        Next
    Next
End Sub
于 2011-04-19T04:06:02.327 に答える