Get Files List From Directory In C#

In this blog, we will create a C# program that prints a list of all files from a particular directory with the file name.
 

Files In Directory

 
Get Files List From Directory In C#
Code
  1. using System;  
  2. using System.IO;  
  3.   
  4. namespace GetFileFromDirectory  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             DirectoryInfo d = new DirectoryInfo(@"E:\Movies");    
  11.             FileInfo[] Files = d.GetFiles();  
  12.             Console.WriteLine("Files in this directory.");  
  13.             Console.WriteLine("---------------------------------------------------------------------------------------");  
  14.             foreach (FileInfo file in Files)  
  15.             {                  
  16.                 Console.WriteLine("File Name : {0}" , file.Name);  
  17.             }  
  18.             Console.ReadKey();  
  19.         }  
  20.     }  
  21. }  

Explanation

  • In the above code, we get directory/folder info bypassing our folder path in DirectoryInfo Constructor.
  • Then we create an array of FileInfo and get all files that are in our directory by our variable d.
  • Then iterate that FileInfo array and print file name one by one.
Output
 
Get Files List From Directory In C#
Next Recommended Reading Saving C# List Into Text File