Sorting Files by Name, Date, FileSize etc (C#)
It is simple to get a list of files in .NET, you simply do: string folder = "c:\\windows\\"; string[] files = Directory.GetFiles(folder); This will get all the files in C:\Windows (does not include subdirectories). However, they are sorted by name (A-Z) and there aren't any options in the GetFiles method to return them in any other order. Luckily, the results are returned as an array and they can be sorted with a custom comparer. I have created a FileComparer class that can be used to sort the files (works in both .NET 1.1 and 2.0): public class FileComparer : IComparer { public enum CompareBy { Name /* a-z */, LastWriteTime /* oldest to newest */, CreationTime /* oldest to newest */, LastAccessTime /* oldest to newest */, FileSize /* smallest first */ } // default comparison int _CompareBy = (int)CompareBy.Name; public FileComparer() { } public FileComparer(CompareBy compareBy) { _CompareBy = (int)compareBy; } int IComparer.Compare( obj...