Use this to get an array of FileInfo objects and then sort the array on FileInfo.CreationTime
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C:\\jpegs");
System.IO.FileInfo[] files = di.GetFiles();
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
You can use this to do the sort.
Array.Sort<FileInfo>(files,
delegate(FileInfo file1 ,FileInfo file2)
{
if (file1 == null)
{
if (file2 == null)
return 0;
else
return -1;
}
else
{
if (file2 == null)
return 1;
else
return file1.CreationTime.CompareTo(file2.CreationTime);
}
});
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
files is already an array. Just add the FileInfo objects to the list box. listBox1.Items.AddRange(files);
Note: To reference the listbox items you will need to cast the object type back to an FileInfo object.
E.g. ((FileInfo)listBox1.Items[0]).FullName
Edit: To have the listbox contain the filename as a string just modify your original foreach loop
foreach (FileInfo fi in files)
{
listBox1.Items.Add(fi.Name);
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187