Solved

VB.Net - shortcut to obtaining most recent file

Posted on 2016-07-18
3
46 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 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 51

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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