C# Get All Filenames and Directory Names in Directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.IO; class Program { static void Main(string[] args) { try { string FolderPath; string[] Files, Folders; //For example C:\Samples Console.Write(" Enter Directory Path: "); FolderPath = Console.ReadLine(); Files = Directory.GetFiles(FolderPath); Folders = Directory.GetDirectories(FolderPath); Console.Write("\n *** The FilePaths in given Directory *** \n"); foreach (string FileName in Files) { Console.WriteLine(FileName); } Console.Write("\n *** The Directories in given Directory *** \n"); foreach (string FolderName in Folders) { Console.WriteLine(FolderName); } Console.ReadLine(); } catch (DirectoryNotFoundException ex) { Console.WriteLine("\n\n Wrong Path!!! Check Directory Name!!! "); Console.ReadKey(); } Console.ReadLine(); } } |
Output:
In folder