引入命名空间
using System.IO;
using System.Collections;
public class FileComparer : IComparer
{
int IComparer.Compare(Object o1, Object o2)
{
FileInfo fi1 = o1 as FileInfo;
FileInfo fi2 = o2 as FileInfo;
return fi1.CreationTime.CompareTo(fi2.CreationTime);
}
}
public class MyClass
{
public FileInfo[] GetFiles(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] files = di.GetFiles();
FileComparer fc = new FileComparer();
Array.Sort(files, fc);
return files;
}
}
使用
FileInfo[] a = new MyClass().GetFiles(@"D:\data");