我正在借用这个功能。通过使用该信息填充数据表,它变得非常简单,您可以在需要时进一步操作。
gridview.DataSource = Fileinfo_To_DataTable("mypath")
Private Function Fileinfo_To_DataTable(ByVal directory As String) As DataTable
Try
'Create a new data table
Dim dt As DataTable = New DataTable
'Add the following columns: Name. Length Last Write Time, Creation Time
dt.Columns.Addrange({New DataColumn("Name"), New DataColumn("Length", GetType(Long)), New DataColumn("Last Write Time", GetType(Date)), New DataColumn("Creation Time", GetType(Date))})
'Loop through each file in the directory
For Each file As IO.FileInfo In New IO.DirectoryInfo(directory).GetFiles
'Create a new row
Dim dr As DataRow = dt.NewRow
'Set the data
dr(0) = file.Name
dr(1) = file.Length
dr(2) = file.LastWriteTime
dr(3) = file.CreationTime
'Add the row to the data table
dt.Rows.Add(dr)
Next
'Return the data table
Return dt
Catch ex As Exception
Console.WriteLine(ex.ToString)
'Return nothing if something fails
Return Nothing
End Try
End Function