Hi all,
Can any one tell me, How we can Sort Files by Their Sizes?
Hi all,
Can any one tell me, How we can Sort Files by Their Sizes?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit ChoudharyRank #145 · 15.8K reputationPosted Jul 4, 2011, 9:50 PM EDT
Use below code:
Program that sorts files by size [C#]
using System;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
// Directory of files.
const string dir = "C:\\site";
// File names. string[] fns = Directory.GetFiles(dir);
// Order by size.
var sort = from fn in fns orderby new FileInfo(fn).Length descending
select fn;
// List files.
foreach (string n in sort)
{
Console.WriteLine(n);
}
}
}
Please mark the answer if it helps you.
Thanks.