Recursive Retrieval of Directory Structure in C#

This code snippet will retrive/enlists all files and folders in Directory and sub directories in Recursive traversal. See the Code Snippet below.

C#
</p>
<p>using System;<br />
using System.IO;</p>
<p>namespace ConsoleAppDemo<br />
{<br />
    class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            //List all the directories and files in Drive G:<br />
            GetDirStructure(@"G:");<br />
        }</p>
<p>        #region GetDirStructure<br />
        /// <summary><br />
        /// Method to recursively write out a directory structure<br />
        /// </summary><br />
        /// <param name="path">Directory to start at</param><br />
        public static void GetDirStructure(string path)<br />
        {<br />
            try<br />
            {<br />
                //retrives directory info<br />
                DirectoryInfo dir = new DirectoryInfo(path);<br />
                //get all sub directories of the main directory<br />
                DirectoryInfo[] subDirs = dir.GetDirectories();<br />
                //gets all files in the Parent directory( if any)<br />
                FileInfo[] files = dir.GetFiles();</p>
<p>                foreach(FileInfo fi in files)<br />
                {<br />
                    Console.WriteLine("--> " + fi.FullName.ToString());<br />
                }<br />
                //if subdirectories are available<br />
                if (subDirs != null)<br />
                {<br />
                    foreach (DirectoryInfo sd in subDirs)<br />
                    {<br />
                        //Recursive Call To Same method to enlist sub directory contents<br />
                        GetDirStructure(path + @"\" + sd.Name);<br />
                    }<br />
                }<br />
            }<br />
            catch(Exception ex)<br />
            {<br />
                Console.WriteLine(ex.Message.ToString());<br />
            }<br />
        }<br />
#endregion<br />
    }<br />
}</p>
<p>

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT SQL_CALC_FOUND_ROWS cmxg4_comments.comment_ID FROM cmxg4_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1394 AND comment_type NOT IN ('note') ORDER BY cmxg4_comments.comment_date_gmt ASC, cmxg4_comments.comment_ID ASC

Leave a comment

Your email address will not be published. Required fields are marked *

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'akismet_comment_nonce' LIMIT 1

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'cookie_consent_template' LIMIT 1

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT option_value FROM cmxg4_options WHERE option_name = 'jpsq_sync_checkout'

WordPress database error: [User 'dataadl336' has exceeded the 'max_questions' resource (current value: 40000)]
SELECT COUNT(*) FROM cmxg4_jetpack_sync_queue WHERE queue_id = 'sync'