如何使用.NET在目录中find最近的文件,并且没有循环?
我需要在目录中find最近修改的文件。
我知道我可以遍历文件夹中的每个文件并比较File.GetLastWriteTime
,但有没有更好的方法来做到这一点没有循环?
怎么样这样的事情…
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();
如果你想recursionsearch,你可以使用这段美丽的代码:
public static FileInfo GetNewestFile(DirectoryInfo directory) { return directory.GetFiles() .Union(directory.GetDirectories().Select(d => GetNewestFile(d))) .OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime)) .FirstOrDefault(); }
只需按照以下方式调用它:
FileInfo newestFile = GetNewestFile(new DirectoryInfo(@"C:\directory\"));
就是这样。 返回FileInfo
实例,如果目录为空,则返回null
。
扩展上面的第一个,如果你想search一个特定的模式,你可以使用下面的代码:
string pattern = "*.txt" var dirInfo = new DirectoryInfo(directory); var file = (from f in dirInfo.GetFiles(pattern) orderby f.LastWriteTime descending select f).First();
非LINQ版本:
/// <summary> /// Returns latest writen file from the specified directory. /// If the directory does not exist or doesn't contain any file, DateTime.MinValue is returned. /// </summary> /// <param name="directoryInfo">Path of the directory that needs to be scanned</param> /// <returns></returns> private static DateTime GetLatestWriteTimeFromFileInDirectory(DirectoryInfo directoryInfo) { if (directoryInfo == null || !directoryInfo.Exists) return DateTime.MinValue; FileInfo[] files = directoryInfo.GetFiles(); DateTime lastWrite = DateTime.MinValue; foreach (FileInfo file in files) { if (file.LastWriteTime > lastWrite) { lastWrite = file.LastWriteTime; } } return lastWrite; } /// <summary> /// Returns file's latest writen timestamp from the specified directory. /// If the directory does not exist or doesn't contain any file, null is returned. /// </summary> /// <param name="directoryInfo">Path of the directory that needs to be scanned</param> /// <returns></returns> private static FileInfo GetLatestWritenFileFileInDirectory(DirectoryInfo directoryInfo) { if (directoryInfo == null || !directoryInfo.Exists) return null; FileInfo[] files = directoryInfo.GetFiles(); DateTime lastWrite = DateTime.MinValue; FileInfo lastWritenFile = null; foreach (FileInfo file in files) { if (file.LastWriteTime > lastWrite) { lastWrite = file.LastWriteTime; lastWritenFile = file; } } return lastWritenFile; }
这有点晚了,但…
您的代码将无法正常工作,因为list<FileInfo> lastUpdateFile = null;
后来lastUpdatedFile.Add(file);
所以NullReferenceexception将被抛出。 工作版本应该是:
private List<FileInfo> GetLastUpdatedFileInDirectory(DirectoryInfo directoryInfo) { FileInfo[] files = directoryInfo.GetFiles(); List<FileInfo> lastUpdatedFile = new List<FileInfo>(); DateTime lastUpdate = DateTime.MinValue; foreach (FileInfo file in files) { if (file.LastAccessTime > lastUpdate) { lastUpdatedFile.Add(file); lastUpdate = file.LastAccessTime; } } return lastUpdatedFile; }
谢谢
您可以使用FileSystemWatcher响应新的文件活动。
这是一个从每个子目录获取最新文件的版本
List<string> reports = new List<string>(); DirectoryInfo directory = new DirectoryInfo(ReportsRoot); directory.GetFiles("*.xlsx", SearchOption.AllDirectories).GroupBy(fl => fl.DirectoryName) .ForEach(g => reports.Add(g.OrderByDescending(fi => fi.LastWriteTime).First().FullName));
private List<FileInfo> GetLastUpdatedFileInDirectory(DirectoryInfo directoryInfo) { FileInfo[] files = directoryInfo.GetFiles(); List<FileInfo> lastUpdatedFile = null; DateTime lastUpdate = new DateTime(1, 0, 0); foreach (FileInfo file in files) { if (file.LastAccessTime > lastUpdate) { lastUpdatedFile.Add(file); lastUpdate = file.LastAccessTime; } } return lastUpdatedFile; }
我这样做是一堆我的应用程序,我用这样的语句:
var inputDirectory = new DirectoryInfo("\\Directory_Path_here"); var myFile = inputDirectory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
从这里你将获得“inputDirectory”variables的目录中最近保存/添加/更新的文件的文件名。 现在你可以访问它,并做它你想要的。
希望有所帮助。
简短的版本:
new DirectoryInfo([directoryPath]).GetFiles().OrderByDescending(x => x.LastWriteTime).FirstOrDefault();