How to read folder and subfolder files?

5

I have a function to read the files from the folder, and it is not working. I need a functional function that reads files from folders and subfolders. Here is the code:

FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();
            txtArquivo.Text = fbd.SelectedPath.ToString();

            //Marca o diretório a ser listado
            DirectoryInfo diretorio = new DirectoryInfo(txtArquivo.Text);
            //Executa função GetFile(Lista os arquivos desejados de acordo com o parametro)
            FileInfo[] Arquivos = diretorio.GetFiles("*.xml; *xlsx;");

            string[] files = System.IO.Directory.GetFiles(fbd.SelectedPath);
    
asked by anonymous 05.09.2014 / 19:31

1 answer

11

If you are using at least .NET 4.0 you basically need this:

Directory.EnumerateFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories))

Documentation

Or you can use it from version 2.0:

Directory.GetFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories))

Documentation

That is, what you needed to know is the overload with the searchOptions .

If you want to get an extension, just change the search pattern from "*.*" to the extension you want. If you want to get more than one extension you will have to use the solution that I already indicated in my other answer .

There is a complete example of Microsoft using another approach.

    
05.09.2014 / 19:49