简体   繁体   English

如何获取最新(最后修改)的目录[C#]

[英]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.

相关问题 C#:从目录中获取 5 个最新(最后修改)的文件 - C#: Get the 5 newest (last modified) files from a directory 如何在c#中获取该目录中最后修改的特定TYPE文件的日期和时间 - how get the date and time of the last modified particular TYPE file in that directory in c# 如何在C#中仅使用System.IO命名空间中的GetFiles获取目录中最新创建的文件 - how to get the newest created file in a directory using only GetFiles in System.IO Namespace in C# 如何在C#中压缩最后修改的文件 - How to compress last modified file in C# 如何在C#中的路径字符串中获取倒数第二个目录 - How to get the second to last directory in a path string in C# 如何从ftp URL获取最后的目录C# - How to get the last directory from a ftp url C# C#读取目录中的最新文件 - C# Reading newest file in directory 如何在使用C#移动文件时获取文件的上次修改日期 - How to get last modified date of file while moving it using C# 如何获取 Azure blob 存储中目录的“上次修改”属性 - How to get "Last Modified" property for Directory in Azure blob storage 如何根据 C# 中的最后修改日期从两个数据表中获取最新记录行? - How to get the latest record rows from a two datatable based on last modified date in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM