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
Comments (0)