11

Is there a way I can store the file location of the 5 last modified files from a directory using Array?

I am currently using the following codes below to get the last file:

DateTime lastHigh = new DateTime(1900,1,1);
string highDir;
foreach (string subdir in Directory.GetDirectories(path)){
    DirectoryInfo fi1 = new DirectoryInfo(subdir);
    DateTime created = fi1.LastWriteTime;

    if (created > lastHigh){
        highDir = subdir;
        lastHigh = created;
    }
}

I'll be using Array to send multiple files to an email address as attachment.

UPDATE

I am currently using the codes below to get the last modified files after 1 minute:

string myDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
                  "Test Folder");
var directory = new DirectoryInfo(myDirectory);
DateTime from_date = DateTime.Now.AddMinutes(-1);
DateTime to_date = DateTime.Now;
var files = directory.GetFiles().Where(file => file.LastWriteTime >= from_date && file.LastWriteTime <= to_date);

I want to store to list of file names coming from files

abramlimpin
  • 5,027
  • 11
  • 58
  • 97
  • 1
    Are you just needing ".ToArray()" on the end of the files definition? ie `var files = directory.GetFiles().Where(file => file.LastWriteTime >= from_date && file.LastWriteTime <= to_date).ToArray();` – Michael Jul 09 '12 at 01:35
  • 1
    @Michael I keep getting the error "Cannot implicitly convert type 'string' to 'System.IO.FileInfo'" – abramlimpin Jul 09 '12 at 01:47
  • 1
    You have to create a `FileInfo` object from a path. Use the appropriate constructor, `new FileInfo(path)` – Paul Phillips Jul 09 '12 at 01:50

3 Answers3

26

Here's a general way to do this with LINQ:

 Directory.GetFiles(path)
             .Select(x => new FileInfo(x))
             .OrderByDescending(x => x.LastWriteTime)
             .Take(5)
             .ToArray()

I suspect this isn't quite what you want, since your code examples seem to be working at different tasks, but in the general case, this would do what the title of your question requests.

Paul Phillips
  • 6,093
  • 24
  • 34
  • How can I get the Lastest modified Directory on the same way? – VarunJi Apr 08 '14 at 08:01
  • 2
    While this answer works, I think the answer at http://stackoverflow.com/questions/1179970/c-sharp-find-most-recent-file-in-dir might be better. – newman May 21 '14 at 04:07
1

It sounds like you want a string array of the full filepaths of all the files in a directory.

Given you already have your FileInfo enumerable, you can do this:

var filenames = files.Select(f => f.FullName).ToArray();

If you wanted just the filenames, replace FullName with Name.

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
1

While the answer Paul Phillips provided worked. It's worth to keep in mind that the FileInfo.LastWriteTime & FileInfo.LastAccessTime do not always work. It depends on how the OS is configured or could be a caching issue.

.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong

File.GetLastWriteTime seems to be returning 'out of date' value

Osama Rizwan
  • 615
  • 1
  • 7
  • 19
Luv2Learn
  • 130
  • 1
  • 1
  • 9