我有一个TreeView,我需要动态填充。递归填充(winforms)Treeview
内容与目录结构相似(请参阅附图)。
现在,为了获取这些'文件夹',我使用了一个只列出'顶级'文件夹的命令(参考图片)。 (请注意,这不是操作系统目录/文件夹..我只是使用目录/文件夹比喻来使事情可以理解)
因此,对于例如我有Root,Folder1,Sub_Folder1,Sub_Folder2,Sub-subfolder_1,Folder2,然后用'/'选项发出命令会给我一个列表:Folder1,Folder2。
如果我需要的Level-2文件夹(Sub_Folder_1和Sub_Folder_2),我需要再次发出带有选项“/资料夹”命令..
我需要反复发出这些命令,直到我得到的最后一个子..文件夹并使用列表来填充TreeView。
我使用下面的C#(4.5)代码,但我只能列出2级。
任何帮助纠正将不胜感激!
try
{
BuildInfaCmd(InfaCmdType.ListFolders, folder);
InfaCmd icmd = CallInfaCmd(InfaCmdExe, InfaCmdArgs);
if (icmd.ExitCode() == 0)
{
List<string> folders = icmd.GetFolders();
if (folders.Count > 0)
topFolderFound = true;
foreach (string f in folders)
{
if (node == null) // Add to 'root' of Treeview
{
TreeNode p = new TreeNode(f);
treeView1.Nodes.Add(p);
PopulateFoldersRecursive(f, null);
}
else
{
callLvl += 1;
//MessageBox.Show("Calling recursive " + callLvl.ToString());
TreeNode p = new TreeNode(f);
node.Nodes.Add(p); // Add to calling node as children
string fold = node.Text + "/" + f; // The sub-folder to be provided to ListFolder command like -p /RootFolder/SubFolder1/SubFolder2/...
PopulateFoldersRecursive(fold, p, callLvl);
}
}
}
else
{
MessageBox.Show(icmd.GetError(), "Error while executing InfaCmd", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
此代码是'PopulateFoldersRecursive'方法吗?你调试了代码吗?任何调试结果?什么是'CallInfaCmd'? –
我认为这个问题已经回答,这个链接可能会帮助你[链接](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – Haxsta
可能重复[填充TreeView与文件系统目录结构](https://stackoverflow.com/questions/6239544/populate-treeview-with-file-system-directory-structure) – GuidoG