在标签vb.net中循环文件名

时间:2013-02-13 11:58:45

标签: vb.net file loops directory

使用vb.net,我如何遍历给定目录中的所有文件名,然后将其显示在标签中?

Dim PATH_DIR_1 As String
Dim INTERVAL_DIR_1 As String

PATH_DIR_1 = Registry.GetValue("HKEY_CLASSES_ROOT\SOFTWARE\Sidewinder", "DIR_1", "")

INTERVAL_DIR_1 = Registry.GetValue("HKEY_CLASSES_ROOT\SOFTWARE\Sidewinder", "INT_DIR_1", "")

For Each foundFile As String In (PATH_DIR_1)
    Label1.Text = (foundFile)
Next

3 个答案:

答案 0 :(得分:0)

您可以使用以下命令获取目录的文件名列表:

Dim sFiles() as String = System.IO.Directory.GetFiles(sDirectoryPath)

然后按照您想要的方式将其添加到Label中:

For Each s As String in sFiles
    Label1.Text &= s & "/"
Next

答案 1 :(得分:0)

System.IO有许多用于处理Windows文件系统的类,以下是您要执行的操作的示例:

Imports System.IO

......

Sub DisplayFileList(ByRef theLabel As Label, ByVal thePath As String)

    Dim di As New DirectoryInfo(thePath)

    For Each fi As FileInfo In di.GetFiles()

        theLabel.Text &= fi.FullName 'or just fi.Name, FullName is the complete path

    Next

End Sub

答案 2 :(得分:0)

或者

Imports System.IO
...
For Each filePathAsString In Directory.EnumerateFiles("DirPath")
   Label1.Text = filePathAsString
Next