get files from directory
-
Use the following line of code to get list of files. string[] files = Directory.GetFiles(@"C:\docs"); now iterate in your own fashion to descending order.
Balaji
-
DirectoryInfo dir = new DirectoryInfo(@"c:\");
FileInfo[] files = dir.GetFiles();Array.Reverse(files);
foreach(FileInfo file in files)
{
Console.WriteLine(file.Name);
}Console.ReadLine();
Hi, AFAIK the order of the files returned by GetFiles() depends on the underlying file system; NTFS would sort alphabetically, FAT/FAT32 would not. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
-
Hi, AFAIK the order of the files returned by GetFiles() depends on the underlying file system; NTFS would sort alphabetically, FAT/FAT32 would not. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google
I didn't realize that FAT/FAT32 weren't ordered. My bad. Implement a IComparer class then. You can change the compare method to sort however you want, by date if you wanted. Maybe even pass in an Enum of sort options.
using System;
using System.Collections;
using System.IO;public class CompareFileInfo : IComparer
{
int IComparer.Compare(object first, object second)
{
FileInfo file1 = (FileInfo)first;
FileInfo file2 = (FileInfo)second;return string.Compare(file1.Name, file2.Name); }
}
public class MyClass
{
public static void Main()
{
DirectoryInfo dir = new DirectoryInfo(@"c:\");
FileInfo[] files = dir.GetFiles();
Array.Sort(files, new CompareFileInfo());
Array.Reverse(files);
foreach(FileInfo file in files)
{
Console.WriteLine(file.Name);
}
}
}