>I have a folder with many Excel files. What would be the most elegant way to get the filename of the newest file? I know I can cycle through all the files and do a compare on the dates, but since I am going to be running this on a web server I am afraid that will incur too much overhead.
>
>Thanks for any advice!
Wish it were 2.0:) You can use Array.Sort with an IComparer. ie:
DirectoryInfo di = new DirectoryInfo(stringPath);
FileInfo[] fi = di.GetFiles("*.xls");
MyComparer mc = new MyComparer();
Array.Sort(fi, mc);
public class MyComparer:IComparer
{
public int Compare(object x, object y)
{
return ((FileInfo)y).LastWriteTime.CompareTo( ((FileInfo)x).LastWriteTime );
}
}
Cetin