Sabari M Sabari M
Updated date Feb 22, 2021
Using C#, you can easily get the list of files from the directory and subdirectories. This code snippet walks you through the steps of how to retrieve the files from the specified directory using C#.

Using C#, you can easily get the list of files from the directory and subdirectories. This code snippet walks you through the steps of how to retrieve the files from the specified directory using C#.

Get the list of files from a specific directory (Including the files from the subdirectory):

using System;
using System.IO;
namespace Test
{
    class SampleProgram
    {
        static void Main()
        {
            // Retrieve the list of files from the specific directory          
            string[] files = Directory.GetFiles("E:\\Sabari\\", "*.*", SearchOption.AllDirectories);

            // Display all the files.
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
            Console.ReadKey();
        }
    }
}

Output:

E:\Sabari\Test File.rar
E:\Sabari\Test Directory1\Test Sub-directory1\Publisher 1.pub
E:\Sabari\Test Directory1\Test Sub-directory1\Test Dir1\Document 1.docx
E:\Sabari\Test Directory1\Test Sub-directory1\Test Dir2\Notepad File 1.txt
E:\Sabari\Test Directory1\Test Sub-directory1\Test Dir2\Notepad File 2.txt
E:\Sabari\Test Directory1\Test Sub-directory2\Worksheet.xlsx
E:\Sabari\Test Directory2\File 1.bmp

Get the files from the specific directory:

using System;
using System.IO;
namespace Test
{
    class SampleProgram
    {
        static void Main()
        {
            // Retrieve the list of files from the specific directory          
            string[] files = Directory.GetFiles(@"E:\Sabari\");

            // Display all the files.
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
            Console.ReadKey();
        }
    }
}

Output:

E:\Sabari\Test File.rar

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 14+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!