LINQ to Files and Directories

In this example we will write LINQ queries on directories to get the gif images and its size order by image size.

LINQ Query:

Below LINQ query will find the gif image in a folder order by image size.

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"E:\output"); var giffiles = from file in dir.GetFiles() where file.Extension == ".gif" orderby file.Length select new { FileName = file.Name, FileSize = (file.Length / 1024) + " KB" }; gv.DataSource = giffiles; gv.DataBind();

Output:

Linq to files