Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to List all Files and Directories in FTP Server using Python?
Next article icon

C# Program to Search Directories and List Files

Last Updated : 01 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 SearchOption

It will take two fields that is AllDirectories and TopDirectoryOnly. AllDirectories field is used to perform the search that contains the current directory and all its subdirectories in a search operation. And TopDirectoryOnly field is used to search only in the main directory.

2. GetFiles: This method is used to return the name of the files present in a particular directory or subdirectory. Or we can say that it returns the name along with the path of the files that are available in the given directory. 

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

1. Read the directory and search in C drive A folder using searchoption AllDirectories keyword

list = Directory.GetFiles("C:\\A\\","*.*", SearchOption.AllDirectories)

2. Iterate through the list and display using foreach loop

foreach (string file in list)  {      Console.WriteLine(file);  }

Example: 

In this example, we are displaying the list of file names along with their path by searching in the C drive - A directory.

C#
// C# program to search directories and list files  using System; using System.IO;  class GFG{      static void Main() {          // Here we search the file present in C drive     // and A directory. Using SearchOption      string[] list = Directory.GetFiles("C:\\A\\", "*.*",                                         SearchOption.AllDirectories);                                             // Display the file names      // Present in the A directory      foreach (string file in list)     {         Console.WriteLine(file);     } } } 

Output:

C:\A\file.txt

Next Article
How to List all Files and Directories in FTP Server using Python?

M

manojkumarreddymallidi
Improve
Article Tags :
  • C#
  • CSharp-programs

Similar Reads

  • C# Program For Listing the Files in a Directory
    Given files, now our task is to list all these files in the directory using C#. So to do this task we use the following function and class: DirectoryInfo: It is a class that provides different types of methods for moving, creating, and enumerating through directories and their subdirectories. You ca
    2 min read
  • Python List All Files In Directory And Subdirectories
    Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
    5 min read
  • How to List all Files and Directories in FTP Server using Python?
    FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network.  It is possible using pyt
    2 min read
  • Obtain List of Directories in R
    A directory or folder in the R programming language may contain other directories. It is possible to access the list of all the directories using base methods in R and return them as a list of folders.  The list.dirs() method in R language is used to retrieve a list of directories present within the
    3 min read
  • Shell Script to List all Hidden Files in Current Directory
    Here we are going to see how to write a script to list all hidden files in the current directory, But before starting we will see how to hide the file in the current directory. Hide Files in Linux: In Linux, the files which start with a period (.) sign are the hidden files. We will write a small scr
    2 min read
  • Read all Files in Directory using R
    To list all files in a directory in R programming language we use list.files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories. If no files are present in the directory,
    2 min read
  • Python - Get list of files in directory with size
    In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a
    9 min read
  • Python - List files in directory with extension
    In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python. Modules Usedos: The OS module in Python provides functions for interacting with the operating system.glob: In Python, the glob module is used to retrieve fi
    3 min read
  • Python - Get list of files in directory sorted by size
    In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language. The two different approaches to get the list of files in a directory are sorted by size is as follows: Using os.listdir(
    3 min read
  • C# Program to Check the Information of the File
    Given a file, now our task is to view the information of this file through C#. So to do this task we use FileInfo class. This class provides different types of properties and methods for creating, copying, deleting, opening, and moving files. It is also, used for creating FileStream objects. So here
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences