C# Program For Listing the Files in a Directory
Last Updated :
15 Nov, 2021
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
Similar Reads
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
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
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
How to Count Files in Directory Recursively in Linux When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
5 min read