how to list all files names under a folder in hard drive?

梦想与她 提交于 2019-12-24 03:58:05

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/46091557/how-to-list-all-files-names-under-a-folder-in-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!