Net get files in directory asp.net top

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#

using System;

using System.IO;

DirectoryInfo_object.GetFiles()

0

DirectoryInfo_object.GetFiles()

1

DirectoryInfo_object.GetFiles()

2

DirectoryInfo_object.GetFiles()

3

DirectoryInfo_object.GetFiles()

4

DirectoryInfo_object.GetFiles()

5

DirectoryInfo_object.GetFiles()

6

DirectoryInfo_object.GetFiles()

7

DirectoryInfo_object.GetFiles()

8

DirectoryInfo_object.GetFiles()

9

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

0

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

1

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

2

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

3

DirectoryInfo_object.GetFiles()

8

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

5

DirectoryInfo_object.GetFiles()

8

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

7

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

8

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

3

DirectoryInfo_object.GetFiles()

8

        FileInfo[] Files = place.GetFiles();
1

DirectoryInfo_object.GetFiles()

8

        FileInfo[] Files = place.GetFiles();
3
        FileInfo[] Files = place.GetFiles();
4
        FileInfo[] Files = place.GetFiles();
5
        FileInfo[] Files = place.GetFiles();
6

DirectoryInfo_object.GetFiles()

8

DirectoryInfo_object.GetFiles()

7

        FileInfo[] Files = place.GetFiles();
9

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

7

foreach(FileInfo i in Files) {

Console.WriteLine("File Name - {0}",
                   i.Name);
}

1

foreach(FileInfo i in Files) {

Console.WriteLine("File Name - {0}",
                   i.Name);
}

2

DirectoryInfo_object.GetFiles()

8

foreach(FileInfo i in Files) {

Console.WriteLine("File Name - {0}",
                   i.Name);
}

4

foreach(FileInfo i in Files) {

Console.WriteLine("File Name - {0}",
                   i.Name);
}

4

foreach(FileInfo i in Files) {

Console.WriteLine("File Name - {0}",
                   i.Name);
}

4

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

How to get all the files in a directory in C#?

For knowing the name of the files that are present in the specified directory we use the GetFiles() method. public static string[] GetFiles (string path); We can use GetFiles() and GetDirectories() to know the files and subdirectories in the specified directory.

Does directory GetFiles return folders?

GetFiles(String, String) Returns the names of files (including their paths) that match the specified search pattern in the specified directory.

How to extract filename from path in C#?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

How to list all the files in a directory and subdirectories?

To list all files in the current directory, type the following: ls -a This lists all files, including. dot (.) ... .

To display detailed information, type the following: ls -l chap1 .profile. ... .

To display detailed information about a directory, type the following: ls -d -l ..