How to read folder and subfolder files?

I have a function to read the files from the folder, and it is not working. I need a functional function that reads the files from the folders and subfolders. Follow 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);
Author: Maniero, 2014-09-05

1 answers

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

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

Documentation

Or can use since version 2.0:

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

Documentation

That is, what you needed to know is the overhead with the parameter of searchOptions.

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

There is a complete Microsoft example using another approach.

 12
Author: Maniero, 2020-09-08 16:30:21