[英]How to get the newest (last modified) directory [C#]
Currently my application uses string[] subdirs = Directory.GetDirectories(path) to get the list of subdirectories, and now I want to extract the path to the latest (last modified) subdirectory in the list. 目前我的应用程序使用string [] subdirs = Directory.GetDirectories(path)来获取子目录列表,现在我想提取列表中最新(最后修改过的)子目录的路径。
What is the easiest way to accomplish this? 实现这一目标的最简单方法是什么? (efficiency is not a major concern - but robustness is)
(效率不是一个主要问题 - 但坚固性是)
Non-recursive: 非递归:
new DirectoryInfo(path).GetDirectories()
.OrderByDescending(d=>d.LastWriteTimeUtc).First();
Recursive: 递归:
new DirectoryInfo(path).GetDirectories("*",
SearchOption.AllDirectories).OrderByDescending(d=>d.LastWriteTimeUtc).First();
without using LINQ 不使用LINQ
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;
}
}
Be warned: You might need to call Refresh()
on your Directory Info object to get the correct information: 警告:您可能需要在Directory Info对象上调用
Refresh()
以获取正确的信息:
eg in Laramie's answer you'd edit to: 例如,在Laramie的答案中,你要编辑为:
DirectoryInfo fi1 = new DirectoryInfo(subdir);
fi1.Refresh();
DateTime created = fi1.LastWriteTime;
Otherwise you might get outdated info like I did: 否则你可能会像我一样得到过时的信息:
"Calls must be made to Refresh before attempting to get the attribute information, or the information will be outdated."
“在尝试获取属性信息之前必须进行刷新,否则信息将过时。”
http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.refresh(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.refresh(v=vs.71).aspx
Try this: 尝试这个:
string pattern = "*.txt"
var dirInfo = new DirectoryInfo(directory);
var file = (from f in dirInfo.GetFiles(pattern)
orderby f.LastWriteTime descending
select f).First();
http://zamirsblog.blogspot.com/2012/07/c-find-most-recent-file-in-directory.html http://zamirsblog.blogspot.com/2012/07/c-find-most-recent-file-in-directory.html
You can use Directory.GetLastWriteTime (or Directory.GetLastWriteTimeUtc
, it doesn't really matter in this case when you're just doing relative comparisons). 您可以使用Directory.GetLastWriteTime (或
Directory.GetLastWriteTimeUtc
,在这种情况下,当您进行相对比较时,它并不重要)。
Although do you just want to look at the "modified" time as reported by the OS, or do you want to find the directory with the most recently-modified file inside it? 虽然你只是想看看“修改”时间由OS报道,或者你想找到它里面的最近修饰文件的目录? They don't always match up (that is, the OS doesn't always update the containing directory "last modified" time when it modifies a file).
它们并不总是匹配(也就是说,OS修改文件时并不总是更新包含目录“上次修改”的时间)。
If you are building a windows service and you want to be notified when a new file or directory is created you could also use a FileSystemWatcher . 如果要构建Windows服务,并且希望在创建新文件或目录时收到通知,则还可以使用FileSystemWatcher 。 Admittedly not as easy, but interesting to play with.
不可否认,这并不容易,但有趣。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.