I have a TreeView which I need to populate dynamically.
The contents would be similar to directory structure (Refer attached pic).
Now, in order to fetch these 'folders' I use a command, which would list out only 'top-level' folders (refer pic). (Please note this is not OS directory / folders.. I am only just using directory / folder analogy to make things understandable)
So, for e.g. I have Root, Folder1, Sub_Folder1, Sub_Folder2, Sub-sub_folder_1, Folder2, then issuing command with a '/' option will give me a list: Folder1, Folder2.
If I need Level-2 folders (Sub_Folder_1 and Sub_Folder_2), I again need to issue the command with option "/Folder1"..
I need to repeatedly issue these commands, until I get the last sub.. folder and use the list to populate a TreeView.
I am using the below C# (4.5) code, but I am able to list only 2-levels.
Any help in correcting would be much appreciated!
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);
}
}