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 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