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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Jul 5, 2011, 1:50 AM
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.