Create A IComparer class .Using Directory.GetFiles() method we can use only one search parameter at a time.
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);
}
}
Code behind file code
string[] strFile = Directory.GetFiles(@strPath, "*.jpg");
IComparer fileComparer = new CompareFileByDate();
Array.Sort(strFile, fileComparer);
foreach (string fi in strFile)
Method 2:
string[] fns = Directory.GetFiles(@strPath);
listDocs.Items.Clear();
// Order by size.
var sort = from fn in fns
where fn.Contains(".jpg") || fn.Contains(".txt") || fn.Contains(".png")
orderby new FileInfo(fn).CreationTime descending
select fn;
foreach (string obj in sort)
{
listDocs.Items.Add(obj.Substring(obj.LastIndexOf("\\") + 1));
}
0 comments:
Post a Comment