Loading, please wait...

A to Z Full Forms and Acronyms

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 and file size.

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

 

 

 

Code

using System;  
using System.IO;  
  
namespace GetFileFromDirectory  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            DirectoryInfo d = new DirectoryInfo(@"E:\Movies");    
            FileInfo[] Files = d.GetFiles();  
            Console.WriteLine("Files in this directory.");  
            Console.WriteLine("----------------------------------------------");  
            foreach (FileInfo file in Files)  
            {                  
                Console.WriteLine("File Name : {0}" , file.Name);  
            }  
            Console.ReadKey();  
        }  
    }  
} 

 

Explanation

  • In above code we get directory/ folder info by passing our folder path in DirectoryInfo Constructor.
  • Then we create an array of FileInfo and get all files which are in our directory by our variable d.
  • Then iterate that FileInfo array and print file name one by one.

Output

 

 

Source :  My Website

 

A to Z Full Forms and Acronyms