【问题标题】:Get files from sub directory using vb.net使用 vb.net 从子目录获取文件
【发布时间】:2013-11-28 17:05:59
【问题描述】:

我有一个目录,其中包含许多文件夹,例如 folder1,folder2,folder3 等..其中包含子目录..其中我有一个文件夹名称 "special"其中包含一些文件

现在我想根据子目录的名称获取所有这些文件

例子:

C:\Users\desktop\Myfolder\folder1\special\
C:\Users\desktop\Myfolder\folder2\special\
C:\Users\desktop\Myfolder\folder3\special\
C:\Users\desktop\Myfolder\folder4\special\

现在我需要从所有folder1、folder2、folder3和folder4的每个特殊文件夹中获取所有文件,并将它们显示在gridview中。

【问题讨论】:

  • based on the name of the sub-directory 您是否要按特定顺序获取文件?
  • 没有类似的......子目录在某种意义上的特殊文件夹名称。

标签: vb.net file datagrid directory


【解决方案1】:
grid1.DataSource = (From p1 In IO.Directory.GetFiles("C:\Users\desktop\Myfolder\", "*", IO.SearchOption.AllDirectories)
                    Where p1.Contains("\special\"))

grid1.DataBind()

【讨论】:

    【解决方案2】:

    我刚刚处理了您的案例,我认为以下代码将满足您的要求。给定的代码将遍历目录并显示文件名(如果它位于special 目录下)。如果我错误地回答了您的问题,请回复我。

    程序,

    Private Sub GetFiles(ByVal xPath As String)
    
            Try
    
                If Directory.GetDirectories(xPath).Length > 0 Then
                    For Each xDir As String In Directory.GetDirectories(xPath)
                        If Directory.Exists(xDir) Then
                            GetFiles(xDir)
                        End If
                    Next
                End If
    
                If Directory.GetFiles(xPath).Length > 0 Then
                    For Each xDir As String In Directory.GetFiles(xPath)
                        If UCase(Path.GetDirectoryName(xDir)).EndsWith("SPECIAL") Then
                            MsgBox(Path.GetFileName(xDir))
                        End If
                    Next
                End If
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
        End Sub
    

    还有电话,

     call GetFiles("D:\test")
    

    【讨论】:

    • @K3rnel31 不过,我在使用螺母和螺栓时感到很满意。
    【解决方案3】:

    如果您的 datagridview 是 datagridview1 并且包含两列并且您想添加名称文件并且最后修改这里是解决方案..

        For Each sDir In Directory.GetDirectories("C:\Users\desktop\Myfolder\", "special", SearchOption.AllDirectories)
            For Each File In Directory.GetFiles(sDir)
                Dim detailedfile As New IO.FileInfo(File)
                DataGridView1.Rows.Add(detailedfile.Name, detailedfile.LastAccessTime)
            Next
    

    下一个

    如果您想在网格视图中添加更多详细信息,您只需在 the DataGridView1.Rows.Add 中添加更多 columns 和更多整数

    【讨论】:

    • 你能告诉我如何将文件名添加到gridview吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2012-06-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    相关资源
    最近更新 更多