1.

Can we display all the files in the Directory in C#?

Answer»

Yes, we can display all the files in a specific DIRECTORY in C#. We have to use the DirectoryInfo class. Within that set the directory from where you WANT to fetch all the files:

DirectoryInfo d = new DirectoryInfo(@"D:\SID");

The GetFiles() method is then used to get all the files in an array:

FileInfo [] fileInfo = d.GetFiles(); foreach (FileInfo f in fileInfo) {   Console.WriteLine("File Name: ", f.Name); }

The following is an example:

using SYSTEM; using System.IO; namespace Application {   class Example {      static void Main(string[] args) {         DirectoryInfo d = new DirectoryInfo(@"D:\sid");         FileInfo [] fileInfo = d.GetFiles();         foreach (FileInfo f in fileInfo) {            Console.WriteLine("File Name: ", f.Name);         }         Console.ReadKey();      }   } }

The output displays the files in the “sid” directory:

NewOne NewTwo NewThree NewFour


Discussion

No Comment Found