Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to get all files and sub folders from specific path in C#

1 Answer

0 votes
using System;
using System.IO;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //              or new DirectoryInfo("c:\\..."); 
            DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); 

            FullDirList(di, "*.*");
            foreach (FileInfo f in files)
            {
                Console.WriteLine(f.FullName);
            }
            foreach (DirectoryInfo fl in folders)
            {
                Console.WriteLine(fl.FullName);
            }
        }
        
        static List files = new List();  
        static List folders = new List();

        static void FullDirList(DirectoryInfo dir, string search)
        {
            try
            {
                foreach (FileInfo f in dir.GetFiles(search))
                {
                    files.Add(f);
                }
            }
            catch
            {
                Console.WriteLine("Directory {0} - could not be accessed!", dir.FullName);
                return;  
            }
            foreach (DirectoryInfo d in dir.GetDirectories())
            {
                folders.Add(d);
                FullDirList(d, search);
            }
        }
    }
}


answered Apr 10, 2014 by avibootz
edited Jan 30, 2016 by avibootz

Related questions

...