Solved

VB.Net - shortcut to obtaining most recent file

Posted on 2016-07-18
3
29 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
3 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 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 49

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now