How do I read all the files in a folder sorted by Created Date Time (Using System.IO).

P

Peri

Dear All,

I am developing 2 applications.

The first application will keep on generate a new file in a span of 30
milliseconds with some valid data inside (This code is written in C).

The second application (A Windows Service written in VB.NET) will keep on
read the file from the same folder and update the database. My problem is
that the second appliaction will have to read the files in the same order
(Created Date Time in milliseconds) in which the fisrt application has
written.

I tried using System.IO.Directory and System.IO.DirectoryInfo. In this I am
able to get the file CreatedTime but not able to order it by the Created
Time and then do the processing. As of now I am reading the File, taking the
created time, putting this in a array, sorting it by DateTime and then
processing the file.

Is there any way I can do this in VB.NET/C# ?

Thanks and Regards,

Peri
 
Z

zacks

Dear All,

I am developing 2 applications.

The first application will keep on generate a new file in a span of 30
milliseconds with some valid data inside (This code is written in C).

The second application (A Windows Service written in VB.NET) will keep on
read the file from the same folder and update the database. My problem is
that the second appliaction will have to read the files in the same order
(Created Date Time in milliseconds) in which the fisrt application has
written.

I tried using System.IO.Directory and System.IO.DirectoryInfo. In this I am
able to get the file CreatedTime but not able to order it by the Created
Time and then do the processing. As of now I am reading the File, taking the
created time, putting this in a array, sorting it by DateTime and then
processing the file.

Is there any way I can do this in VB.NET/C# ?

Here is some VB.NET 2005 code I do it with. Note in my case, I am
sorting the list DESCENDING.

Friend Sub Purge_Log(ByVal sDirectory As String, ByVal
sSearchPattern As String)
'
' process the purging of old logs based on the
Logging.NumberToKeep property
' NOTE: The current log file is not actually created unless a
record is actually written to it
'
Dim colLogFiles As New List(Of clsLogFile)
Dim fi As FileInfo
Dim myLogFile As clsLogFile
Dim sFiles() As String
Dim sFile As String
Dim i As Integer

sFiles = Directory.GetFiles(sDirectory, sSearchPattern)
For Each sFile In sFiles
fi = New FileInfo(sFile)
myLogFile = New clsLogFile
myLogFile.FileName = fi.FullName
myLogFile.DateCreated = fi.CreationTime
colLogFiles.Add(myLogFile)
Next
colLogFiles.Sort(AddressOf CompareDateCreated)
i = 0
For Each myLogFile In colLogFiles
i += 1
If i > myDTC.Logging.NumberToKeep Then
If File.Exists(myLogFile.FileName) Then
File.Delete(myLogFile.FileName)
If myDTC.Logging.LoggingLevel =
clsDataTransferConfiguration.LoggingLevels.TESTING Or
myDTC.Logging.LoggingLevel =
clsDataTransferConfiguration.LoggingLevels.VERBOSE Then
Message = "Purged Log File " &
myLogFile.FileName & vbCrLf
MyLog.LogInfo(Message)
End If
End If
End If
Next

End Sub

Friend Function CompareDateCreated(ByVal x As clsLogFile, ByVal y
As clsLogFile) As Integer

If x.DateCreated > y.DateCreated Then Return -1
If x.DateCreated < y.DateCreated Then Return 1
Return 0

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top