文件系统TreeView

时间:2009-03-23 15:51:36

标签: c# file treeview path system

我正在使用文件系统,我有一个List<>将文件路径作为属性的文件对象。基本上我需要在.NET中创建一个树视图,但我很难想到最好的方法去做这个,因为我需要从列表中创建树结构,如:

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

等...

列表根本没有结构,我无法对当前的对象结构进行任何更改。

我在C#工作。

非常感谢所有贡献者

5 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:4)

我会将字符串转换为FileInfo。

拥有FileInfo对象后,您可以使用Directory属性为每个路径检索DirectoryInfo

一旦你有了路径的DirectoryInfo,就可以很容易地“走上”DirectoryInfo中的Parent引用,将每个路径转换为目录列表+ filename - 即:

{"C:","Windows","Temp","ErrorLog.txt"}

这应该非常简单,可以插入到树视图中。只需依次查看路径的每个部分,如果它不存在,请添加....

答案 2 :(得分: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;
}

这段代码应该只给你一个想法 - 我必须承认我在发布之前没有测试过它。

答案 3 :(得分: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];
            }
        }
    }

alt text

答案 4 :(得分:0)

EHosca的作品完美地为我服务,只有一个改变 - 我必须在路径区域的foreach路径之后将lastnode设置为空。

这是上面的eHosca代码,移植到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