【发布时间】:2019-09-09 20:57:54
【问题描述】:
我正在使用treeView在表单上的treeView中显示序列目录及其子目录和文件,我使用以下方法加载树视图
在表单加载中:
treeView1.Nodes.Clear();
toolTip1.ShowAlways = true;
LoadDirectory("C:\\Windows\\System32\\" + inventedName );
以及以下3种加载目录和子目录及文件的方法
public void LoadDirectory(string Dir)
{
DirectoryInfo di = new DirectoryInfo(Dir);
TreeNode tds = treeView1.Nodes.Add(di.Name);
tds.Tag = di.FullName;
//tds.StateImageIndex = 0;
tds.ImageIndex = 0;
tds.StateImageIndex = 0;
tds.SelectedImageIndex = 0;
LoadFiles(Dir, tds);
LoadSubDirectories(Dir, tds);
}
private void LoadSubDirectories(string dir, TreeNode td)
{
string[] subdirectoryEntries = Directory.GetDirectories(dir);
// Loop through them to see if they have any other subdirectories
foreach (string subdirectory in subdirectoryEntries)
{
DirectoryInfo di = new DirectoryInfo(subdirectory);
TreeNode tds = td.Nodes.Add(di.Name);
renameNodes(tds);
//tds.StateImageIndex = 0;
tds.Tag = di.FullName;
tds.ImageIndex = 0;
tds.StateImageIndex = 0;
tds.SelectedImageIndex = 0;
LoadFiles(subdirectory, tds);
LoadSubDirectories(subdirectory, tds);
}
}
private void LoadFiles(string dir, TreeNode td)
{
string[] Files = Directory.GetFiles(dir, "*.pdf");
// Loop through them to see files
foreach (string file in Files)
{
FileInfo fi = new FileInfo(file);
TreeNode tds = td.Nodes.Add(fi.Name);
tds.Tag = fi.FullName;
tds.ImageIndex = 1;
tds.StateImageIndex = 1;
tds.SelectedImageIndex = 1;
}
}
我的问题是子目录(文件夹)有特定的名称,我无法更改它 例如:
> root
> parent
> 1.0 xxx
> 1.10 xxx
> 1.2 xxx
> 1.3 xxx
> 1.4 xxx
> 1.5 xxx
> 1.6 xxx
> 1.7 xxx
> 1.8 xxx
> 1.9 xxx
但我需要它是那样的
> root
> parent
> 1.0 xxx
> 1.2 xxx
> 1.3 xxx
> 1.4 xxx
> 1.5 xxx
> 1.6 xxx
> 1.7 xxx
> 1.8 xxx
> 1.9 xxx
> 1.10 xxx
愚蠢的(1.10 xxx)孩子必须在(1.9 xxx)孩子之后 正如我所说,我不能重命名错误的文件夹是否有任何方法可以将其发送为最后一个孩子
谢谢你帮助我
【问题讨论】:
-
您应该对文件列表进行排序。 In this post 你可以在更新部分找到一个函数,将字符串扩展为可排序的数字..
-
亲爱的@vik_78 我试过了,但它不适合我
-
亲爱的@TaW 感谢您的评论,但我使用的是treeView,该方法适用于列表视图
-
我知道。代码可以教你做什么。而且,正如我所写的,这实际上与 TreeView 无关,而是关于获得一个适当排序的列表,您可以从中创建节点..
标签: c# winforms treeview nodes appearance