如何以字符串形式返回目录中的最新文件?

发布于 2024-11-25 17:43:33 字数 167 浏览 0 评论 0原文

我希望能够将最新项目(已创建)作为程序中的字符串返回,

例如 s = test.txt

“下载”目录

text.txt Date created 4/5/2011
something.txt Date created 1/1/2011

I would like to be able to return the newest item (that has been created) as a string within a program

e.g. s = test.txt

the "download" directory

text.txt Date created 4/5/2011
something.txt Date created 1/1/2011

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一枫情书 2024-12-02 17:43:33

怎么样

Directory.EnumerateFiles("directory").
  OrderBy(f => File.GetCreationTime(f)).Last()

How about

Directory.EnumerateFiles("directory").
  OrderBy(f => File.GetCreationTime(f)).Last()
灯角 2024-12-02 17:43:33
string res = Directory.EnumerateFiles(direcory)
    .OrderByDescending(f => new FileInfo(f).CreationTime).FirstOrDefault();
string res = Directory.EnumerateFiles(direcory)
    .OrderByDescending(f => new FileInfo(f).CreationTime).FirstOrDefault();
九八野马 2024-12-02 17:43:33

基于 MSDN 中的代码片段

string startFolder = @"c:\Download\";
// Take a snapshot of the file system.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

//Create the query
IEnumerable<System.IO.FileInfo> fileQuery =
    from file in fileList
    where file.Extension == ".txt"
    orderby file.Name
    select file;

// Create and execute a new query by using the previous 
// query as a starting point. fileQuery is not 
// executed again until the call to Last()
var newestFile =
    (from file in fileList
     orderby file.CreationTime
     select new { file.FullName, file.CreationTime })
    .Last();

Console.WriteLine("\r\nThe newest .txt file is {0}. Creation time: {1}",
    newestFile.FullName, newestFile.CreationTime);

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();

Based on the code snippet from MSDN

string startFolder = @"c:\Download\";
// Take a snapshot of the file system.
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);

// This method assumes that the application has discovery permissions
// for all folders under the specified path.
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

//Create the query
IEnumerable<System.IO.FileInfo> fileQuery =
    from file in fileList
    where file.Extension == ".txt"
    orderby file.Name
    select file;

// Create and execute a new query by using the previous 
// query as a starting point. fileQuery is not 
// executed again until the call to Last()
var newestFile =
    (from file in fileList
     orderby file.CreationTime
     select new { file.FullName, file.CreationTime })
    .Last();

Console.WriteLine("\r\nThe newest .txt file is {0}. Creation time: {1}",
    newestFile.FullName, newestFile.CreationTime);

// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文