Check last modified date of file in C#
System.IO.File.GetLastWriteTime is what you need.
System.IO.File.GetLastWriteTime is what you need.
As already mentioned by others, a better way to solve this would be to trigger a function when particular events happen, that changes the folder. However, if your server is a unix, you can use inotifywait to watch the directory, and then invoke a PHP script. Here’s a simple example: #!/bin/sh inotifywait –recursive –monitor –quiet … Read more
private File getLatestFilefromDir(String dirPath){ File dir = new File(dirPath); File[] files = dir.listFiles(); if (files == null || files.length == 0) { return null; } File lastModifiedFile = files[0]; for (int i = 1; i < files.length; i++) { if (lastModifiedFile.lastModified() < files[i].lastModified()) { lastModifiedFile = files[i]; } } return lastModifiedFile; }
how about something like this… var directory = new DirectoryInfo(“C:\\MyDirectory”); var myFile = (from f in directory.GetFiles() orderby f.LastWriteTime descending select f).First(); // or… var myFile = directory.GetFiles() .OrderByDescending(f => f.LastWriteTime) .First();