C# Program to Search Sub-Directory in a Given Directory Last Updated : 24 Mar, 2022 Comments Improve Suggest changes Like Article Like Report C# is a general-purpose, object-oriented programming language pronounced as “C Sharp”. It is a lot syntactically similar to Java and is easy for users who have knowledge of C, C++, or Java. In this article, we will learn How can we use C# to search the sub-Directory in a given Directory. So for this task, we use the following methods: 1. SearchOption: This method is used to tell the compiler whether to do our search in the current directory or the current directory with all subdirectories. Syntax: public enum SearchOptionIt will take two fields: AllDirectories: This is used to perform the search that contains the current directory and all its subdirectories in a search operation. TopDirectoryOnly: This is used to search only in the main directory. 2. GetFiles: When we need to fetch the names of the files present in a directory or subdirectory then the GetFiles function is used. It returns a string array containing the names of the files. Syntax: public static string[] GetFiles (string path);Where the path is the directory to search. This string is not case-sensitive. Here the path can be the relative or absolute path. Approach:Using SearchOption we will find all the files in a subdirectory.Then using GetFiles, we will extract all those files that are present in the directory and initialize them to a string array.Now simple traverse that string array using for each loop.Using conditional operator we will match whether file names are equal or not. Return "yes" if the file is found else "no"Example 1: C# // C# code for search a subdirectory // C: -> GFG -> // Here only 1 file i.e Test.txt using System; using System.IO; class GFG { static void Main() { // Here we search the file present in C drive // and GFG directory. Using SearchOption string[] list = Directory.GetFiles("C:\\GFG\\", "*.*", SearchOption.AllDirectories); string value = "GFG.txt"; // File to be searched int flag = 0; // Search the file names // Present in the A directory foreach(string file in list) { if (file == value) { flag = 1; break; } } if (flag == 1) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } } } Output: noExample 2: C# // C# code for search subdirectory C: -> GFG -> using System; using System.IO; class GFG { static void Main() { // Here we search the file present in C drive // and GFG directory. Using SearchOption string[] list = Directory.GetFiles("C:\\GFG\\", "*.*", SearchOption.AllDirectories); string value = "Test.txt"; // File to be searched int flag = 0; // Search the file names // Present in the A directory foreach(string file in list) { if (file == value) { flag = 1; break; } } if (flag == 1) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } } } Output: yes Comment More infoAdvertise with us Next Article C# Program to Search Sub-Directory in a Given Directory A akshitsaxenaa09 Follow Improve Article Tags : C# C# Programs Geeks Premier League Geeks-Premier-League-2022 CSharp-File-Handling Similar Reads C# Program to Get Root Directory of Given Directory Directory class provides different types of methods for creating, moving, deleting, renaming, and modifying directories and subdirectories. GetDirectoryRoot() is a method of Directory class. This method is used to find the information of the volume or root or both for the given path. Or we can say t 2 min read C# Program to Get the List of Sub-Directories of a Given Directory Given a director, now we will find the list of the sub-directories present in the given directory. So to this task, we use the GetDirectories() method of the Directory class. This method is used to get the list of directories/sub-directories from the given directory or sub-directories. We have to sp 2 min read C# Program to Get the List of Files From Given Directory Given a directory, now we will find the list of files from the given directory. So for this, we use the GetFiles() method of the Directory class. This method is used to find the list of files from the given directory or sub directories. The overloaded methods of this method are: 1. GetFiles(String): 2 min read C# Program to Check Given Directory Exists or not Given a directory, now our task is to check given directory exists or not. So to this task, we use the Exists() method of the Directory class. This method will return true if the given directory exists, otherwise false. Syntax: public static bool Exists (string? Mypath); Where, Mypath is a parameter 2 min read C# Program to Create a Directory A directory is a file system that stores file. Now our task is to create a directory in C#. We can create a directory by using the CreateDirectory() method of the Directory class. This method is used to create directories and subdirectories in a specified path. If the specified directory exists or t 2 min read C# Program to Get Complete Path of Current Directory Given a directory, now our task is to find the path of the given directory or current directory. So to this task, we use the GetCurrentDirectory() method of the Directory class. This method will return the complete path of the current directory. The result given by this method will not end with a ba 1 min read C# Program to Demonstrate the Use of CreateSubdirectory Method DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc. This class has a CreateSubdirectory() method that is used to create a sub-directory or sub-directories on the given path. Here the 2 min read C# Program to Demonstrate the Use of FullName Property DirectoryInfo class provides different types of methods and properties that are used to perform operations on directories and sub-directories like creating, moving, etc and FullName property is one of them. This property is used to find the full path of the directory or the specified file. Syntax: p 1 min read C# Program to Show the Use of Exists Property DirectoryInfo class provides different types of methods and properties for creating, moving, deleting, renaming, and modifying directories and subdirectories. Exists property is the property of DirectoryInfo class. This property is used to check whether a directory exists or not and return the boole 1 min read C# Program to Search Directories and List Files Given files and directories, now our task is to search these files and directories using C#. So to do this task we use the following methods: 1. SearchOption: This method is used to specify whether to search the current directory or the current directory with all subdirectories. Syntax: public enum 2 min read Like