How do I find the most recently modified file in a directory uing vb.net

I have written a VB.net function that lists all of the files in a specific directory of a specific type, however, I need to be able to return the name of the file that was most recently modified. My function is shown below.
Private Function getNewestFile(ByVal sDir As String) As String
    Dim newestFile As string
    Dim f As String
 
    Try
 
      For Each f In Directory.GetFiles(sDir, "*.txt")
        'here I need to get the contents of the newest .txt file in this dir
      Next
      Return newestFile
    Catch excpt As System.Exception
      Debug.WriteLine(excpt.Message)
    End Try
  End Function

Open in new window

Viking46Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nicksbellCommented:
Try this:
Private Function getNewestFile(ByVal sDir As String) As String
        Dim dtNewestTime As Date
        Dim dtFileTime As Date
        Dim fNewest As FileInfo
        Try
            Dim dir As New DirectoryInfo(sDir)
            For Each f As FileInfo In dir.GetFiles("*.txt")
                dtFileTime = f.LastWriteTime
                If dtFileTime.Subtract(dtNewestTime).TotalMilliseconds > 0 Then
                    dtNewestTime = dtFileTime
                    fNewest = f
                End If
            Next
            Return fNewest.FullName
        Catch excpt As System.Exception
            Debug.WriteLine(excpt.Message)
        End Try
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Viking46Author Commented:
Thank you nicksbell, just what I needed:)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you have VB.Net 3.0 (or above) then you can use LINQ:

        Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        Dim LastModifiedFile As System.IO.FileInfo = (From fi In (New System.IO.DirectoryInfo(folder)).GetFiles("*.txt") Select fi Order By fi.LastWriteTime Descending Take 1)(0)

        Debug.Print("Last Modified File:")
        Debug.Print(LastModifiedFile.FullName)
        Debug.Print(LastModifiedFile.LastWriteTime)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
See "Language-Integrated Query (LINQ) - Basic Query Operations (Visual Basic)":
http://msdn.microsoft.com/en-us/library/bb384504.aspx
0
Viking46Author Commented:
Hello again Idle_Mind and thank ypu for your input. LINQ is something that I havent heard of before and which leads to an elegant solution to this problem. It is in the style of SQL and therefore intuitive.

Thank you once again. Yout input is most appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.