Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

Please advice how to list the sub-directories and files present inside the sub-directories in listbox using C#.
Posted
Comments
Sergey Alexandrovich Kryukov 26-Jun-14 14:58pm    
What's wrong with just reading original MSDN documentation?
—SA

Please see my comment to the question. How about reading this: http://msdn.microsoft.com/en-us/library/system.io.directory.getdirectories%28v=vs.110%29.aspx[^]?

—SA
 
Share this answer
 
Comments
goathik 26-Jun-14 15:26pm    
THAT ^ is the answer.
abhicoolhot 3-Jul-14 14:11pm    
In my case I have parent directory and inside that I have many sub-directories. On my button click parent directory list down all the files and directories but when I click the sub-directories it's not listing anything
goathik 3-Jul-14 14:59pm    
I had made a small project that lists all files. I needed to do that to list a collection of 1 tb of mp3 that i had on my portable HD. When I read this enquery, I searched for that project but I could not find it :( Ill try again...
here you are. try it and let me know if you need anything else:

C#
private void Form1_Load(object sender, EventArgs e)
        {
            var sb = new StringBuilder();
            DirSearch(@"C:\Users\vitor\Documents\Visual Studio 2012\Projects\WindowsFormsApplication2", sb);

            //save on a txt file
            File.WriteAllText(@"C:\Users\vitor\Desktop\output.txt", sb.ToString());
        }

        static string ultimaLinha = "";
        static void DirSearch(string sDir, StringBuilder sb)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    string u = d.Substring(0, d.LastIndexOf("\\"));
                    if (u != ultimaLinha)
                    {
                        //files
                        foreach (string file in Directory.GetFiles(d))
                            sb.AppendLine(file);
                        //directory only
                        //sb.AppendLine(d);
                    }
                    ultimaLinha = u;
                    DirSearch(d, sb);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900