Open In App

C# Program For Listing the Files in a Directory

Last Updated : 15 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 cannot inherit it.

Syntax:

DirectoryInfo object = new DirectoryInfo(path);

Where path is the file destination for example – @”C:\MyFolder\file_name”.

GetFiles: This method is used to get the list of files that are present in the current directory. The filenames are returned in this method in an unsorted way. If you want sorted file names then use the Sort method.

Syntax:

DirectoryInfo_object.GetFiles()

This method returns an array of type FileInfo. And throws DirectoryNotFoundException when the specified path is not found or is invalid. This method can be overloaded in the following ways:

  • GetFiles(String): This method is used to get the files’ names including their paths in the given directory.
  • GetFiles(String, String, EnumerationOptions): This method is used to get files names along with their paths that match the given search pattern and enumeration options in the given directory.
  • GetFiles(String, String, SearchOption): This method is used to get the file’s names along with their paths that match the given search pattern in the given directory. Also using a value to check whether to search subdirectories.

Approach

1. Create and read the directory using DirectoryInfo class

DirectoryInfo place  = new DirectoryInfo(@"C:\Train");

2. Create an Array to get all list of files using GetFiles() Method

            FileInfo[] Files = place.GetFiles();

3. Display file names with Name attribute through foreach loop

foreach(FileInfo i in Files)
{           
    Console.WriteLine("File Name - {0}",
                       i.Name);
}

Example: 

In this example, we are taking C drive one folder(directory) named Train – It includes all csv files. Now we will display the list of files present in this directory.

C#




// C# program to listing the files in a directory
using System;
using System.IO;
 
class GFG{
     
static void Main(string[] args)
{
     
    // Get the directory
    DirectoryInfo place = new DirectoryInfo(@"C:\Train");
     
    // Using GetFiles() method to get list of all
    // the files present in the Train directory
    FileInfo[] Files = place.GetFiles();
    Console.WriteLine("Files are:");
    Console.WriteLine();
     
    // Display the file names
    foreach(FileInfo i in Files)
    {
        Console.WriteLine("File Name - {0}", i.Name);
    }
}
}


Output:

Files are:
File Name - crop_yielding.csv
File Name - cropdamage.csv
File Name - crops_data.csv
File Name - doses.csv
File Name - pesticides.csv
File Name - soiltype.csv

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads