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?
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

0

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!
0
José PerezAuthor Commented:
btw is there someway to cancel it? :(
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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
0
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

1
José PerezAuthor Commented:
It displays an error "Can not convert System.Collections.Generic.IEnumerable<string> into IEnumerable[]" :(
0
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

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.