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

Asked

Viewed 216 times

0

Good people, I’d like a little help

I made a file explorer that aims to map all folders, subfolders and files of these folders and subfolders according to the letter that is selected.

The problem is that when selecting any letter, the program shows the folders that start by the selected letter and always shows any files that are not inside a folder, on the desktop. The result looks like this:

inserir a descrição da imagem aqui

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 so that when selecting the letter, only folders, subfolders and files of both appear ? Thanks for the help

  • What seems to be missing is the code to insert the files, just add to the tree, the files starting with the letter. You have to put an If.

  • @Renatoafonso, then, I putting an if for each letter, I would eliminate the problem of showing the desktop files, which are not inside a folder ?

1 answer

0


A function is missing before this function you put in your reply. This previous function would only run through desktop folders. Here’s an example, of course, to fit your needs, but basically it would be:

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;
}
  • Good afternoon Gabriel. I would like to ask you. The function SearchDesktopDirectories would have a string parameter named letter ? and each button of the program’s home screen would call that function as well?

  • Yes, this letter parameter can be included. As I said in the answer, you must adapt this response to your needs. As you want to filter folders by letter, you must have this letter parameter in Searchdesktopdirectories.

  • Gabriel, I would like to thank you for your help. I made the modifications and managed to show only the desktop folders. Thank you very much !

  • @Agnerribeiro that good guy! If it was helpful that my answer, could you mark as answer to your question? Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.