2

I want to list all files names exist under folder in hard drive with vb.net , and i don't know how.First ,i choose a folder with folderbrowser component, next,i list all files

Here is my code (only for choose a folder)

   dossier_disque.ShowDialog()
    txt_folder.Text = dossier_disque.SelectedPath

for list all files , i tried to use for each , but it's not correct

my code when i tried to list file

        Dim files() As String = Directory.GetFiles(txt_folder.Text)
    For Each a In CStr(files.Count)
        folder_hard.Rows.Add(Directory.GetFiles(txt_folder.Text))
    Next

folder_hard is a grid name txt_folder is a name of a folder path

With this code , the result , i can see only the first file twice in grid

Zied.M
  • 215
  • 4
  • 17
  • 4
    Possible duplicate of [Get a list of all files inside of a directory in vb.net](https://stackoverflow.com/questions/1457525/get-a-list-of-all-files-inside-of-a-directory-in-vb-net) – muffi Sep 07 '17 at 08:31
  • same result , i edit my post – Zied.M Sep 07 '17 at 09:18
  • Is this a homework and you don't know how to do it? ;-) You know, you are looping through a number and not the file-list? E.g. if you have 587 files in your folder, your For-loop returns you 3 chars. '5', '8', '7'! I think, you wanted to loop through the files-array and add them to your DGV. – muffi Sep 07 '17 at 09:25

1 Answers1

2

There is a problem with your for each loop: CStr() converts values into strings. So your for loop is looping through each char in the string of the number of files in the files array. So change it to:

For Each a In files

Then a will be each file name in the files array. If you want to add each to your grid you need to change that line to :

folder_hard.Rows.Add(a)

So this should work:

Dim files() As String = Directory.GetFiles(txt_folder.Text)
For Each a In files
    folder_hard.Rows.Add(a)
Next
Neal
  • 801
  • 1
  • 9
  • 21