I’ve been forced in to using vb.net for a windows service project which scans a folder of xml files, they need to be processed in order of the files modified date & time. Directory.GetFiles() returns an array of filenames in alphabetic order, I could find precious little information on the net so I thought I’d share what I came up with :-
Imports System.IO
Public Class clsCompareFileInfo
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim File1 As FileInfo
Dim File2 As FileInfo
File1 = DirectCast(x, FileInfo)
File2 = DirectCast(y, FileInfo)
Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)
End Function
End Class
Then to use it…
Dim dirinfo As DirectoryInfo
Dim allFiles() As FileInfo
dirinfo = New DirectoryInfo(sSelPath)
allFiles = dirinfo.GetFiles("*.xml")
Array.Sort(allFiles, New clsCompareFileInfo)
For Each fl As FileInfo In allFiles
MessageBox(fl.FullName.ToString())
Next
To give credit where it’s really due I found the solution in c# at http://www.shanebauer.com/Weblog/PermaLink,guid,b9cd286d-e147-4a25-aa49-bde888a11433.aspx. I just converted the source from c# to vb.net.