How can I improve this code?

0

I was developing software where I can pass some folder paths to be shown in Listbox . I can select one of the paths in Listbox to be able to be displayed, but I need it to be in the same order as it is on windows, showing the folders, subfolders and their respective files in Listview . And Sub below worked very well.

Private Sub PegarArquivos(ByRef caminho As String)

    'Pega o nome da pasta e cria um grupo.
    Dim di As New DirectoryInfo(caminho)
    Dim grupo As String = di.Name

    'Pega todos os arquivos soltos na pasta.
    For Each arquivo In FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchTopLevelOnly)
        MontarGrupos(grupo, arquivo)
    Next

    'Pegas as pasta contidas em: caminho
    For Each pastas In FileIO.FileSystem.GetDirectories(caminho, FileIO.SearchOption.SearchTopLevelOnly)

        'Se tiver menos que 2 arquivos na pasta, joga os arquivos na pasta anterior.
        If FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories).Count < 2 Then
            For Each video In FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories)
                MontarGrupos(grupo, arquivo)
            Next

            'Se não chama a sub: PegarArquivos() 
        Else
            PegarArquivos(pastas)
        End If
    Next
End Sub

And I was doing this whenever a path was selected in ListBox which caused a delay in displaying results because of the time it took for this code to run completely when it had many folders and subfolders. So I did it differently, I loaded all the files in a Classe into the Load event of the form and then just display.

Imports System.IO

Public Class MontarOrdemGrupos
    Public grupo As String
    Public classes As New List(Of MontarOrdemGrupos)
    Public arquivos As New List(Of String)

    Public Sub PegarOClasses(ByVal caminho As String)
        'Pega o nome da pasta e cria um grupo.
        Dim di As New DirectoryInfo(caminho)
        Me.grupo = di.Name

        For Each arquivo In FileIO.FileSystem.GetFiles(caminho, FileIO.SearchOption.SearchTopLevelOnly)

            'Pega todos os arquivos soltos na pasta.
            Me.arquivos.Add(arquivo)
        Next

        'Pegas as pasta contidas em: caminho
        For Each pastas In FileIO.FileSystem.GetDirectories(caminho, FileIO.SearchOption.SearchTopLevelOnly)

            'Se tiver menos que 2 arquivos na pasta, joga os arquivos na pasta anterior.
            If FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories).Count < 2 Then
                For Each arquivo In FileIO.FileSystem.GetFiles(pastas, FileIO.SearchOption.SearchAllSubDirectories)
                    Me.arquivos.Add(arquivo)
                Next

                'Se não cria uma nova classe MontarOrdemGrupos e adiciona em classes.
            Else
                Dim c As New MontarOrdemGrupos
                c.PegarOClasses(pastas)
                classes.Add(c)
            End If
        Next
    End Sub
End Class

With this I solved the problem of delay when displaying the information when I selected the path in Listbox . I created a class by path in Listbox that contained the information of all the folder, subfolder and files contained in them and added in a List , and the only thing I needed was a function to recursively read information in classes. But this generated me 2 problems:

1st I carry "unnecessary" folders eg let's suppose I add the following paths C:\...\arquivos and C:\...\arquivos\imagens I would create 2 classes, and everything in the path class C:\...\arquivos\imagens would be in the C:\...\arquivos class. In other words, carrying unnecessary things.

2nd In the first code I created it took up to 5 seconds to run, ie: When it was selected there in Listbox it would take up to 5 seconds for the result to be shown, but now loading the program it takes almost 1min to open with the same folders being loaded.

I could not do it better, so I came here to ask for your help. How can I solve this problem to have a faster Loading and continue with the instant "presentation"?

    
asked by anonymous 18.10.2018 / 21:42

0 answers