Recursively Browse Directories Using ASP.NET VB

I have come across an issue.  I have solved it part of the way.  I needed to display the contents of a directory for the user when they click on a link (a folder).  That wasn’t too much of a problem.  Now I must also display the ‘most recent files’ in a list, but instead of listing all files all the time, I want to be able to just list the 10 most recent files.  There is my problem.  I have searched the Internet. I have found a way to go through the directories.  I have found a way to display the files in the order I need by using a datagrid and a third function.   I just need to limit how many files are displayed.  There are several functions that will accomplish this.  I will share them with you so they are all in one place.  First we have the originating function call:

Public Sub GetFiles(ByVal path As String)
        If File.Exists(path) Then
            ' This path is a file
            ProcessFile(path, path)
        ElseIf Directory.Exists(path) Then
            ' This path is a directory
            ProcessDirectory(path)
        End If
    End Sub

This in turn calls the following 2 functions:

Public Sub ProcessDirectory(ByVal targetDirectory As String)
Dim dirInfo As New DirectoryInfo(targetDirectory)
Dim newfiles() As FileInfo
'need to make this global so file are added in each loop
newfiles = dirInfo.GetFiles()
Dim f As FileInfo
' Process the list of files found in the directory.
'Array.Sort(newfiles, New CompareFileInfoDESC)
For Each f In newfiles
ProcessFile(f.Name, f.DirectoryName)
Next
' Recurse into subdirectories of this directory.
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
For Each subdirectory As String In subdirectoryEntries
ProcessDirectory(subdirectory)
Next
End Sub

AND

Public Sub ProcessFile(ByVal path As String, ByVal dirname As String)
Dim fi As New FileInfo(dirname + "\" + path)
Dim filelink As String
If position <= 1 Then
Dim myDataColumn As DataColumn
myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "Filename"
myfiledt.Columns.Add(myDataColumn)
myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "Creationtime"
myfiledt.Columns.Add(myDataColumn)
End If
Dim row As DataRow
If Not dirname.EndsWith("_vti_cnf") Then
filelink = "" + fi.Name + "
"
row("filename") = filelink
row("creationtime") = fi.CreationTime
myfiledt.Rows.Add(row)
position += 1
End If
datagrid1.DataSource = AlphabeticSort(myfiledt, 1)
datagrid1.DataBind()
End Sub

The last function that I use is the sort function for the datatable. I need to sort the files by creationdate. Here is that code:


Private Function AlphabeticSort(ByVal dtTable As DataTable, ByVal sortOrder As Integer) As DataTable
Dim columnKey As String = "creationtime"
Dim sortDirection As String = ""
Dim sortFormat As String = "{0} {1}"
Select Case sortOrder
Case 0
sortDirection = "ASC"
Case 1
sortDirection = "DESC"
Case Else
sortDirection = "ASC"
End Select
dtTable.DefaultView.Sort = String.Format(sortFormat, columnKey, sortDirection)
Return dtTable.DefaultView.Table
End Function

When I add all these together I get almost what I need. Like I said, it lists all files. I need just the first 10 files to be listed. Somehow I need to add that functionality into the AlphabeticSort function to return the records I need. I thougt I would share with you what I have so far as it may help you. Any feedback is appreciated. Thank you.

Oh I almost forgot, you have to include the following lines of code above the
Public Sub Page_load.... or nothing will work:

Imports System.IO
Public Sub Page_load.....
Dim position As Integer = 1 ' for Public Sub ProcessFile ()
Dim myfiledt As DataTable = New DataTable()
Dim int As Integer = 0