VB 怎么获取添加文件里的所有文件路径和名称
单击确定后在列表里添加选定文件夹下所有的文件路径和名称,包括根目录里的。PrivateSubButton7_Click(ByValsenderAsSystem.Objec...
单击确定后在列表里添加选定文件夹下所有的文件路径和名称,包括根目录里的。
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim dr, MyPath, MyName As String Dim Result As DialogResult = FolderBrowserDialog1.ShowDialog() dr = FolderBrowserDialog1.SelectedPath If (Result = System.Windows.Forms.DialogResult.OK) Then ListBox1.Items.Remove(dr) ListBox1.Items.Add(dr) MyPath = dr + "\" ' Set the path.vbSystem MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry. Do While MyName <> "" ' Start the loop. ' Use bitwise comparison to make sure MyName is a directory. If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then ' Display entry only if it's a directory. MsgBox(MyName) End If ListBox1.Items.Add(MyPath + MyName) MyName = Dir() ' Get next entry. Loop Else FolderBrowserDialog1.SelectedPath = "" End If End Sub 展开
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Dim dr, MyPath, MyName As String Dim Result As DialogResult = FolderBrowserDialog1.ShowDialog() dr = FolderBrowserDialog1.SelectedPath If (Result = System.Windows.Forms.DialogResult.OK) Then ListBox1.Items.Remove(dr) ListBox1.Items.Add(dr) MyPath = dr + "\" ' Set the path.vbSystem MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry. Do While MyName <> "" ' Start the loop. ' Use bitwise comparison to make sure MyName is a directory. If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then ' Display entry only if it's a directory. MsgBox(MyName) End If ListBox1.Items.Add(MyPath + MyName) MyName = Dir() ' Get next entry. Loop Else FolderBrowserDialog1.SelectedPath = "" End If End Sub 展开
2个回答
展开全部
首先将文件显示做了区分:文件夹名称显示folderlist(),文件名称显示filelist():
Private Function folderlist(pathname As String, treeflag As Integer) 'treeflag:0列表 1树形 2树形展开
'文件夹数和名称
Dim sf As String
sf = Dir(pathname, vbDirectory) '获取第一个目录名
foldernum = 0
Do While sf <> ""
' 跳过当前的目录及上层目录
If sf <> "." And sf <> ".." Then
If (GetAttr(pathname + sf) And vbDirectory) = vbDirectory Then
If treeflag = 0 Then
Text2 = Text2 & sf & vbCrLf ' 如果它是一个目录,将其名称添加到列表
ElseIf treeflag = 1 Then
Text2 = Text2 & Treestr & sf & vbCrLf
ElseIf treeflag = 2 Then '树形展开的时候,需要了解子目录,所以还需进行递归调用
Text2 = Text2 & Treestr & sf & vbCrLf
folderlist pathname + sf, treeflag
filelist pathname + sf, treeflag
End If
foldernum = foldernum + 1 '统计文件夹数目
End If
End If
sf = Dir() ' 查找下一个目录
Loop
If treeflag = 0 Then
If foldernum <> 0 Then
Text2 = Text2 & vbCrLf & "======共有" & foldernum & "个文件夹======" & vbCrLf & vbCrLf
Else
Text2 = Text2 & "======无子目录======" & vbCrLf & vbCrLf
End If
ElseIf foldernum <> 0 Then
Text2 = Text2 & Nullstr & vbCrLf & Nullstr & vbCrLf
End If
End Function
Private Function filelist(pathname As String, treeflag As Integer) '0列表 1树形 2树形展开
'文件数和名称
Dim a As String
a = Dir(pathname & "/*.*")
filenum = 0
Do While a <> ""
If treeflag = 0 Then
Text2 = Text2 & a & vbCrLf
Else
Text2 = Text2 & Treestr & a & vbCrLf
End If
filenum = filenum + 1 '统计文件数目
a = Dir
Loop
If treeflag = 0 Then
If filenum <> 0 Then
Text2 = Text2 & "======共有" & filenum & "个文件======"
Else
Text2 = Text2 & "======无文件信息======"
End If
End If
End Function
Private Function folderlist(pathname As String, treeflag As Integer) 'treeflag:0列表 1树形 2树形展开
'文件夹数和名称
Dim sf As String
sf = Dir(pathname, vbDirectory) '获取第一个目录名
foldernum = 0
Do While sf <> ""
' 跳过当前的目录及上层目录
If sf <> "." And sf <> ".." Then
If (GetAttr(pathname + sf) And vbDirectory) = vbDirectory Then
If treeflag = 0 Then
Text2 = Text2 & sf & vbCrLf ' 如果它是一个目录,将其名称添加到列表
ElseIf treeflag = 1 Then
Text2 = Text2 & Treestr & sf & vbCrLf
ElseIf treeflag = 2 Then '树形展开的时候,需要了解子目录,所以还需进行递归调用
Text2 = Text2 & Treestr & sf & vbCrLf
folderlist pathname + sf, treeflag
filelist pathname + sf, treeflag
End If
foldernum = foldernum + 1 '统计文件夹数目
End If
End If
sf = Dir() ' 查找下一个目录
Loop
If treeflag = 0 Then
If foldernum <> 0 Then
Text2 = Text2 & vbCrLf & "======共有" & foldernum & "个文件夹======" & vbCrLf & vbCrLf
Else
Text2 = Text2 & "======无子目录======" & vbCrLf & vbCrLf
End If
ElseIf foldernum <> 0 Then
Text2 = Text2 & Nullstr & vbCrLf & Nullstr & vbCrLf
End If
End Function
Private Function filelist(pathname As String, treeflag As Integer) '0列表 1树形 2树形展开
'文件数和名称
Dim a As String
a = Dir(pathname & "/*.*")
filenum = 0
Do While a <> ""
If treeflag = 0 Then
Text2 = Text2 & a & vbCrLf
Else
Text2 = Text2 & Treestr & a & vbCrLf
End If
filenum = filenum + 1 '统计文件数目
a = Dir
Loop
If treeflag = 0 Then
If filenum <> 0 Then
Text2 = Text2 & "======共有" & filenum & "个文件======"
Else
Text2 = Text2 & "======无文件信息======"
End If
End If
End Function
展开全部
'显示所有文件到LIST列表
Public Sub getAllPic(ByVal strPath As String)
Try
'获取文件列表数组
Dim filename() As String
filename = System.IO.Directory.GetFiles(strPath)
'添加到LIST中
ListBox2.Items.Clear()
Dim i As Integer
For i = 0 To filename.Length - 1
ListBox2.Items.Add(filename(i))
Next i
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("无文件!")
End Try
End Sub
Public Sub getAllPic(ByVal strPath As String)
Try
'获取文件列表数组
Dim filename() As String
filename = System.IO.Directory.GetFiles(strPath)
'添加到LIST中
ListBox2.Items.Clear()
Dim i As Integer
For i = 0 To filename.Length - 1
ListBox2.Items.Add(filename(i))
Next i
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("无文件!")
End Try
End Sub
追问
只获取到了选定文件夹1下的文件名及路径,不能获取选定的文件夹1下的文件夹2里的文件名及路径
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询