-1

Using vb.net, how can I loop through all file names in a given directory and then display them in a label?

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
Steve
  • 213,761
  • 22
  • 232
  • 286
LabRat
  • 1,996
  • 11
  • 56
  • 91
  • You say you want to loop through file names, but your code shows a registry path. Which do you want? – aphoria Feb 13 '13 at 14:22

3 Answers3

0

You can get the list of filenames of a directory with:

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

And then add it in a Label in the way you want:

For Each s As String in sFiles
    Label1.Text &= s & "/"
Next
SysDragon
  • 9,692
  • 15
  • 60
  • 89
0

System.IO has a lot of classes for working with the windows file system, here's an example for what you want to do:

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
Sean Airey
  • 6,352
  • 1
  • 20
  • 38
0

Or

Imports System.IO
...
For Each filePathAsString In Directory.EnumerateFiles("DirPath")
   Label1.Text = filePathAsString
Next
Alexander Van Atta
  • 870
  • 11
  • 34