Iterating an Array of Files in Directory with C#

The usually used method is using of For each loop but with lambda expression in C# its become piece of cake. Following example will show the iterating through directory file.


DirectoryInfo DirectoryName = new DirectoryInfo("C:\\MyFolder");

First way :

FileInfo[] FileCollection = DirectoryName.GetFiles("emp*.zip");
foreach (FileInfo Files in FileCollection)
{
Response.Write( "[-] {0} ({1})" + Files.Name,Files.GetFiles().Count());
}

This code will display files having extention zip in given folder/directory.

Second way :

Array.ForEach(FileCollection,Files => Response.Write(string.Format( "
    [-] {0} ({1})", Files.Name, Files.GetFiles().Count())));