使用 vb.net 从子目录获取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19998814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get files from sub directory using vb.net
提问by coder
I have a directorry which contains many folders such as folder1,folder2,folder3etc..which contains sub-directories..In that I have a folder name "special"which contains some files
我有一个目录,其中包含许多文件夹,例如folder1,folder2,folder3etc..其中包含子目录..因为我有一个"special"包含一些文件的文件夹名称
Now I would like to get all those files based on the name of the sub-directory
现在我想根据子目录的名称获取所有这些文件
Example:
例子:
C:\Users\desktop\Myfolder\folder1\special\
C:\Users\desktop\Myfolder\folder2\special\
C:\Users\desktop\Myfolder\folder3\special\
C:\Users\desktop\Myfolder\folder4\special\
Now I need to get all the files from each special folder of all the folder1,folder2,folder3 and folder4 and display them in gridview.
现在我需要从所有文件夹 1、文件夹 2、文件夹 3 和文件夹 4 的每个特殊文件夹中获取所有文件,并将它们显示在 gridview 中。
回答by K3rnel31
if your datagridview is datagridview1 and countains two columns and you want to add the name file and last modified here is the solution ..
如果您的 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
next
下一个
if you want to add more details in the gridview you have just to add more columnsand more integers in the DataGridView1.Rows.Add
如果您想在 gridview 中添加更多详细信息,您只需在其中添加越来越columns多的整数the DataGridView1.Rows.Add
回答by Rajaprabhu Aravindasamy
I just worked on your case, and i think the following piece of code will suits your requirement. The given code will drill through the directories and it display the filenames if it is comes under the specialdirectory. Comment me back if i wrongly answered your question.
我刚刚处理了您的案例,我认为以下代码将满足您的要求。给定的代码将钻取目录并显示文件名(如果它位于special目录下)。如果我错误地回答了您的问题,请回复我。
The procedure,
步骤,
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
and the call,
和电话,
call GetFiles("D:\test")
回答by Lisa-Berlin
grid1.DataSource = (From p1 In IO.Directory.GetFiles("C:\Users\desktop\Myfolder\", "*", IO.SearchOption.AllDirectories)
Where p1.Contains("\special\"))
grid1.DataBind()

