C# Get files, folders and subfolders path

Hi,
I am trying to develop a little app that retrieves all the files, folders and subfolders for an specific directory, but It is only doing it for the main folder, not subfolders.
Can someone please check what I am doing wrong?
Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace listaEstructura
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnListar_Click(object sender, EventArgs e)
        {
            GetFiles();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void GetFiles()
        {
            List<String> Myfiles = new List<string>();
            string[] allFiles = System.IO.Directory.GetFiles(txtRuta.Text, "*.*");

            if (allFiles.Length > 0)
            {
                try
                {
                    foreach (string filename in allFiles)
                    {
                        this.richTextBox1.Text = string.Join(Environment.NewLine, allFiles);
                    }
                }

                catch (SystemException excpt)
                {
                    this.richTextBox1.Text = excpt.Message;
                }
            }
        }

        private void btnDir_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtRuta.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}

Open in new window

LVL 2
José PerezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You need to pass the 3rd parameter to Directory.GetFiles:

string[] allFiles = System.IO.Directory.GetFiles(txtRuta.Text, "*.*", System.IO.SearchOption.AllDirectories);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
José PerezAuthor Commented:
ups, it was so good that it It now hang my computer :(

Thanks a lot for your quick answer!
José PerezAuthor Commented:
btw is there someway to cancel it? :(
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Tapan PattanaikSenior EngineerCommented:
Hi José Perez,

Please add "SearchOption.AllDirectories" in your code. Please check the below code.


string[] allFiles = System.IO.Directory.GetFiles(txtRuta.Text, "*.*", SearchOption.AllDirectories);

Regards,
Tapan Pattaniak
käµfm³d 👽Commented:
btw is there someway to cancel it?
With that option, no. But you can switch to Directory.EnumerateFiles and be able to do such.

e.g.

private void GetFiles()
{
    List<String> Myfiles = new List<string>();
    IEnumerable[] allFiles = System.IO.Directory.EnumerateFiles(txtRuta.Text, "*.*", System.IO.SearchOption.AllDirectories);

    try
    {
        foreach (string filename in allFiles)
        {
            this.richTextBox1.Text = string.Join(Environment.NewLine, allFiles);
            
            if (someCondition)
            {
              break;
            }
        }
    }

    catch (SystemException excpt)
    {
        this.richTextBox1.Text = excpt.Message;
    }
}

Open in new window

José PerezAuthor Commented:
It displays an error "Can not convert System.Collections.Generic.IEnumerable<string> into IEnumerable[]" :(
käµfm³d 👽Commented:
My fault:  I changed the type incorrectly. Corrected:

IEnumerable<string> allFiles = System.IO.Directory.EnumerateFiles(txtRuta.Text, "*.*", System.IO.SearchOption.AllDirectories);

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.