How to display, in treeview format, only the folders and subfolders, and the contents of both, that are on the desktop?

0

Well, guys, I'd like some help out of favor

I made a file explorer that aims to map all the folders, subfolders and the files of these folders and subfolders according to the letter that is selected. The problem is that by selecting any letter, the program shows the folders that start with the selected letter and always shows any files that are not inside a folder on the desktop.     The result looks like this:

The function I used to show the folders and subfolders and the files of both, is this

        var directoryNode = new TreeNode(directoryInfo.Name);
        var directoryNode = new TreeNode(directoryInfo.Name);
        foreach (var directory in directoryInfo.GetDirectories(letra))
        directoryNode.Nodes.Add(CreateDirectoryNode(directory, letra));

        foreach (var file in directoryInfo.GetFiles())
        directoryNode.Nodes.Add(new TreeNode(file.Name));
        return directoryNode;

How can I make sure that when I select the letter, only the folders, subfolders, and files of both of them appear? Thanks for the help

    
asked by anonymous 17.01.2017 / 06:32

1 answer

0

A function is missing before this function that you put in your answer. This previous function would only scan the folders on the desktop. Here's an example, of course, missing fit your needs, but basically it would:

public void SearchDesktopDirectories()
{
    var desktopDirectories = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
//aqui já pode fazer a pesquisa pela letra que você quer consultar
    foreach(var desktopDirectory in desktopDirectories.GetDirectories(letra))
    {
        //aqui você percorre apenas as pastas do desktop, passando como parâmetro a letra de pesquisa, e com o retorno insere na sua Tree
        CreateDirectoryNode(desktopDirectory, letra);
    }
}

public void CreateDirectoryNode(DirectoryInfo directoryInfo, string letra)
{
    //aqui dentro segue normal o seu método
    var directoryNode = new TreeNode(directoryInfo.Name);
    foreach (var directory in directoryInfo.GetDirectories(letra))
    directoryNode.Nodes.Add(CreateDirectoryNode(directory, letra));

    foreach (var file in directoryInfo.GetFiles())
    directoryNode.Nodes.Add(new TreeNode(file.Name));
    return directoryNode;
}
    
17.01.2017 / 11:23