How to access subDirectories of a Directory in C#

September 12, 2013 0 Comments


A directory or a folder can contains multiple directories or files. If you want to access all those directories which is under in a parent directory Then you should use DirecotryInfo class for accessing subDirectories of a parent Directory.In previous example we had seen that how to create a subDirectory under parent Directory.Now in this example we will seen that how to access all subDirectories under parent Directory.

Logic behind the program is

Step-1 : Create a instance of DirectoryInfo Class with parent directory path.
Step-2 : Now check , If parent directory is exist or not in specified path
Step-3: If Exist then get all subdirectories under parent directory and assign to an array using GetDirectories() method
Step-4 : Items add into List.

NoteGetDirectories() method does not access compressed directories

Lets take an example How to access subDirectories 

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;
using System.IO;

namespace WindowsFormsApplication2
    public partial class Form5 : Form
        public Form5()

        private void button1_Click(object sender, EventArgs e)
            string dirpath = textBox1.Text;
            DirectoryInfo dirinfo = new DirectoryInfo(dirpath);
            if (dirinfo .Exists)
                DirectoryInfo[] subdir = dirinfo.GetDirectories();
                foreach (DirectoryInfo  item in subdir)


How to access subDirectories of a Directory in C#

How to access subDirectories of a Directory in C#

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google