I have a treeview with all the directory and file structure of the PC where the application is running. When I start, I get a list of strings with several routes, for which I need to find the treeview node corresponding to each of them and mark their checkbox.
The problem is that at the beginning of the application, only the units (fixed and removable disks) appear in the treeview and as I expand each node it is completed with its subdirectories (this is what is proposed so as not to overload the beginning of the application). So what I do not find a way to do is, first, expand the nodes until you reach the last level of each route (it can be a file or directory), and second, check your checkbox.
The code I have is the following:
When starting the application:
foreach (DriveInfo unidad in DriveInfo.GetDrives())
{
if (unidad.DriveType != DriveType.Fixed && unidad.DriveType !=
DriveType.Removable)
{
continue;
}
TreeNode raiz = new TreeNode(unidad.Name);
raiz.ToolTipText = "Unidad";
raiz.Tag = unidad.Name;
raiz.ImageIndex = 0;
raiz.Nodes.Add(new TreeNode());
tvArbol.Nodes.Add(raiz);
}
When expanding a node:
DirectoryInfo directorioPadre = new DirectoryInfo(path);
DirectoryInfo[] subDirectorios = directorioPadre.GetDirectories();
foreach (DirectoryInfo subDirectorio in subDirectorios)
{
TreeNode directorioHijo = new TreeNode(subDirectorio.Name);
directorioHijo.Tag = subDirectorio.FullName;
directorioHijo.ImageIndex = 1;
directorioHijo.Nodes.Add(new TreeNode());
directorioHijo.Checked = nodo.Checked;
nodo.Nodes.Add(directorioHijo);
}
List<FileInfo> archivos = new List<FileInfo>();
archivos.AddRange(directorioPadre.GetFiles());
foreach (FileInfo archivo in archivos)
{
TreeNode archivoHijo = new TreeNode(archivo.Name);
archivoHijo.ImageIndex = 2;
archivoHijo.Tag = archivo;
archivoHijo.Checked = nodo.Checked;
nodo.Nodes.Add(archivoHijo);
}