vb.net - 将文件名返回到数据网格 - 子目录
问题描述
我有一个小的 vb.net 函数,它返回文件名和其他数据,效果很好。但是我需要它返回我设置的目录和子目录中的所有文件。我需要它来深入挖掘文件夹,而不仅仅是在我指向的确切文件夹中。如果我设置目录“X:\Userfiles\”,它只会查看那个确切的文件夹,而不是从那里开始并扫描所有子目录。
有什么想法吗?先感谢您。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DataGridView1.DataSource = Fileinfo_To_DataTable(path.Text)
End Sub
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
解决方案
推荐阅读
- ruby-on-rails - 安装 Rails Engine 时,模块名称无法识别
- javascript - 无法解决 redux reducer 函数中的 axios promise
- javascript - 如果元素的子元素具有相同的内容,则合并它们
- ssl - 如何将 ssl 端口 123 上的 url www.test.com 指向默认网站下的子站点?
- excel - Excel VBA:循环浏览 .msg 文件的目录
- java - java - 如何通过Java中的搜索正确请求带有请求参数的映射?
- php - Laravel Nova 中的数组字段
- python - 如何在模拟类的对象上定义任意属性?
- javascript - 如何使用 testace 作为框架在小黄瓜中截屏?
- java - HikariCP 的连接超时