?
Solved

VB.Net - shortcut to obtaining most recent file

Posted on 2016-07-18
3
Medium Priority
?
52 Views
Last Modified: 2016-07-18
Trying to find the most recent file with the following code:

 Dim FileList = New DirectoryInfo(SummaryDir).GetFiles(Fin + "*.xls").OrderByDescending(Function(x) x.CreationTime)
        For Each f As FileInfo In FileList
            lastfile = f.Name
            Exit For
        Next

Is there a way to avoid the for/next to obtain the last filename?
0
Comment
Question by:Member_2_7964482
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 41717080
sure. you can use FirstOrDefault like this:

MessageBox.Show(New DirectoryInfo("C:\temp").GetFiles("*.tmp").OrderByDescending(Function(x) x.CreationTime).FirstOrDefault().Name)

Open in new window

0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41717083
HI,

maybe

        Dim fi As New New DirectoryInfo(SummaryDir)
        Dim files = fi.GetFiles(Fin + "*.xls").ToList
        Dim last = (From file In files Select file Order By file.CreationTime Descending).FirstOrDefault

Open in new window

Regards
0
 

Author Closing Comment

by:Member_2_7964482
ID: 41717418
Perfect solution.  Thank you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question