文件系统TreeView

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

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

等等….

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

我在C#工作。

非常感谢所有贡献者

如果你想坚持使用这样的字符串就可以了…

 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); } } 

我会把字符串变成FileInfo。

获得FileInfo对象后,可以使用Directory属性检索每个路径的DirectoryInfo 。

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

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

插入树视图应该相当简单。 只需依次查看路径的每个部分,如果它不存在,添加它….

尝试递归。

 private void AddFiles() { // Iterate your list with FileInfos here foreach( var fileInfo in new Collection() ) { 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 = .Nodes[directory.Name]; if( rootNode == null ) { rootNode = new TreeNode(directory.Name); // Access your TreeView control here: .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; } 

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

  private void Form1_Load(object sender, EventArgs e) { var paths = new List { @"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 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]; } } } 

替代文字

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