我有以下代码
Public Function ListDirLatest(ByVal Dir As String, ByVal Amount As Integer) As DataTable
Dim dt As DataTable = ListDir(Dir)
If (dt Is Nothing) Then
Return Nothing
Else
Return dt ' This is where i would like to implement the latest x-files logic
End If
End Function
Private Function ListDir(ByVal Dir As String) As DataTable
If Directory.Exists(Dir) Then
Dim dt As DataTable = GetDT()
Dim dirinfo As New DirectoryInfo(Dir)
For Each fsi As FileSystemInfo In dirinfo.GetFileSystemInfos(".txt")
Dim dr As DataRow = dt.NewRow()
dr("FileName") = fsi.Name()
dr("FileDate") = fsi.CreationTime()
Next
Return dt
Else
Return Nothing
End If
End Function
Private Function GetDT() As DataTable
'Create DataTable to hold results
Dim dt As New DataTable("DirList")
Dim st As System.Type = System.Type.GetType("System.String")
dt.Columns.Add("FileName", st)
dt.Columns.Add("FileDate", st)
Return dt
End Function
目前该ListDirLatest
函数将返回目录中的所有文件。如何更改代码以使其仅返回Amount
参数指定的最新“x”文件。
为了澄清我想返回目录中最新的“x”文件。