Raed Sawalha <Ra*********@discussions.microsoft.com> wrote:
I wondering if it possilble to get the directory files ordered by creation date
string [] files = System.IO.Directory.GetFiles(strPath,"*.msg")
can I sort the returned file by creation date?
You need to implement your own IComparer do the specialized sort:
using System;
using System.IO;
using System.Collections;
public class SortFiles
{
public class CompareFileByDate :IComparer
{
int IComparer.Compare(Object a, Object b)
{
FileInfo fia = new FileInfo((string)a);
FileInfo fib = new FileInfo((string)b);
DateTime cta = fia.CreationTime;
DateTime ctb = fib.CreationTime;
return DateTime.Compare(cta, ctb);
}
}
public static void Main()
{
string [] files = System.IO.Directory.GetFiles("..");
IComparer fileComparer = new CompareFileByDate();
Array.Sort(files, fileComparer);
foreach ( string f in files )
{
Console.WriteLine(f);
}
}
}
/J\