Home > Article > Backend Development > How to list all available files in a directory using C#?
First, use the DirectoryInfo object-
//creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit");
Now, use the GetFiles() method to get all the files-
FileInfo [] f = mydir.GetFiles();
To get the list of files in the directory , please try running the following code -
using System; using System.IO; namespace Demo { class Program { static void Main(string[] args) { //creating a DirectoryInfo object DirectoryInfo mydir = new DirectoryInfo(@"d:\amit"); // getting the files in the directory, their names and size FileInfo [] f = mydir.GetFiles(); foreach (FileInfo file in f) { Console.WriteLine("File Name: {0} Size: {1}", file.Name, file.Length); } Console.ReadKey(); } } }
The above is the detailed content of How to list all available files in a directory using C#?. For more information, please follow other related articles on the PHP Chinese website!