【问题标题】:Loop file names in label vb.net [duplicate]标签vb.net中的循环文件名[重复]
【发布时间】:2013-02-13 22:14:49
【问题描述】:

使用 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

【问题讨论】:

  • 你说你想遍历文件名,但你的代码显示了一个注册表路径。你要哪个?

标签: vb.net file loops directory


【解决方案1】:

您可以通过以下方式获取目录的文件名列表:

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

然后以您想要的方式将其添加到Label

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

【讨论】:

    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

      或者

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 2021-07-25
        • 2015-08-15
        相关资源
        最近更新 更多