首页 > 解决方案 > 将文件名返回到数据网格 - 子目录

问题描述

我有一个小的 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

标签: vb.netdatagrid

解决方案


推荐阅读