Login  |  Register
Home | Articles | Downloads | Resources | About
Contact Us | My Resume
   Topics:  ASP.NET  C#  .NET  XML 
Recursive Directory Function

Posted by on Monday, November 17, 2003 (PST)

This function will recursively search through the directories and list all the files under directories.

using System;
using System.IO;

namespace GetList
{
    public class GetDirectoryList
    {
        public GetDirectoryList()
        {
        }

        public void GetList(string Folder)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(Folder);
                DirectoryInfo[] subDirs = dir.GetDirectories();
                FileInfo[] files = dir.GetFiles();

                foreach(FileInfo fi in files)
                {
                    Console.WriteLine(fi.FullName);
                }
               
                if (subDirs != null)
                {
                    foreach (DirectoryInfo sd in subDirs)
                    {
                        GetList(Folder + @"\\" + sd.Name);
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
   
    class Test
    {
        [STAThread]
        static void Main(string[] args)
        {
            // physical path
            string folder = @"C:\Inetpub\wwwroot\";
           
            GetDirectoryList dl = new GetDirectoryList();
            dl.GetList(folder);
        }

    }
}


Add Your Comment

Most Popular Downloads

  • Dynamically create TextBoxes and DropDownList with C# and ASP.NET
    This article will show you how to create TextBoxes and DropDownList dynamically on ASP.NET page and then retrieve user input values from dynamically created controls.

  • Dynamically create Controls in ASP.NET with C#
    This article will show you how to create TextBoxes dynamically on web page and then retrieve user input values from dynamically created TextBoxes “Text” property content value.

  • Dynamically create TextBoxes with C# on Windows Form
    Dynamically create and retrieve TextBoxes data with C# on Windows Form

  •  
     
    Home Articles Downloads Resources About