C# Program to Search Sub-Directory in a Given Directory


Searching for sub-directories in a given directory is a common task in many applications. In C#, we can use the Directory and DirectoryInfo classes provided by the System.IO namespace to perform this task. In this article, we will explore how to write a C# program to search for sub-directories in a given directory.

Method: Using DirectoryInfo.GetDirectories()

The simplest way to search for sub-directories in a given directory is by using the DirectoryInfo.GetDirectories() method. This method returns an array of DirectoryInfo objects that represent the directories within a specified directory.

Here's how we can use the DirectoryInfo.GetDirectories() method to search for sub-directories in a given directory −

Example

using System.IO;
using System;

class Program {
   static void Main(string[] args) {
      string path = @"C:\Users\JohnDoe\Documents";
      DirectoryInfo directory = new DirectoryInfo(path);
   
      foreach (DirectoryInfo subdirectory in directory.GetDirectories()) {
         Console.WriteLine(subdirectory.FullName);
      }
   }
}

In this code, we start by specifying the directory path that we want to search in a string variable named path. We then create a new DirectoryInfo object that represents the directory.

Next, we use the DirectoryInfo.GetDirectories() method to get an array of DirectoryInfo objects that represent the sub-directories within the directory.

Finally, we use a foreach loop to iterate through the array of sub-directory objects and print the full name of each sub-directory to the console.

Method: Using Directory.GetDirectories()

Another way to search for sub-directories in a given directory is by using the Directory.GetDirectories() method. This method returns an array of strings that represent the names of the sub-directories within a specified directory.

Here's how we can use the Directory.GetDirectories() method to search for sub-directories in a given directory −

Example

using System;
using System.IO;

class Program {
   static void Main() {
      string path = @"C:\Users\JohnDoe\Documents";
   
      string[] subdirectories = Directory.GetDirectories(path);
   
      foreach (string subdirectory in subdirectories) {
         Console.WriteLine(subdirectory);
      }
   }
}

In this code, we start by specifying the directory path that we want to search in a string variable named path. We then use the Directory.GetDirectories() method to get an array of strings that represent the names of the sub-directories within the directory.

Finally, we use a foreach loop to iterate through the array of sub-directory names and print each name to the console.

Conclusion

In this article, we explored how to write a C# program to search for sub-directories in a given directory using the Directory and DirectoryInfo classes provided by the System.IO namespace. We learned how to use the DirectoryInfo.GetDirectories() method and the Directory.GetDirectories() method to search for sub-directories in a given directory. Both methods are efficient and easy to use, making them essential tools for any C# developer.

Updated on: 04-May-2023

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements