C# - Cascade Repeat Structure

Asked

Viewed 35 times

0

first good morning!

I have a task to feed a "file tree (windows explorer style)" in object format.

For this I have initially a method that lists all the files and folders in the directory root of the file system, and based on the typing of these (which may vary between Pasta and Arquivo) I must feed the property with the contents of that folder.

My problem is that I can only feed the first layer of this object, leaving its sub-folders still empty. That is, my call while does not descend to the other levels. Can anyone tell me what I did wrong?

    public async Task<List<Arquivo>> ListarArquivosEPastas()
    {
        try
        {
            var arquivos = await ListarArquivosOuPastas("/");

            foreach (var arquivo in arquivos)
            {
                while (arquivo.Tipo == "Pasta" && arquivo.Conteudo is null)
                {
                    arquivo.Conteudo = await ListarArquivosOuPastasPorIdDiretorio(arquivo.Id);
                }
            }

            return arquivos;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Illustration of how the return is coming: inserir a descrição da imagem aqui

  • without seeing the code of the other methods and tbm knowing what to return in the variable files becomes quite difficult to help

  • @Ricardopunctual They basically return a new array with the files and folders from that directory. I edited the post with an attached image of how this return is in detail

  • look at this tree structure the secret will be in the method ListarArquivosOuPastasPorIdDiretorio, that there must be a recursive call, and also the class that will have this data will need if a kind of linked list, otherwise it will not get out of the first level. It needs to be something like Class Arvore { Pasta Pasta {get; set; } and Class Pasta { Pasta PastaPai { get; set; } List<Arquivo> Arquivos { get; set; } }

  • If you have questions, let us know, then put an answer

No answers

Browser other questions tagged

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