问题描述
我想使用vb.net列出硬盘驱动器中文件夹下所有存在的文件名,我不知道如何。首先,我选择一个包含folderbrowser组件的文件夹,接下来,我列出所有文件
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是网格名称
txt_folder是a的名称文件夹路径
folder_hard is a grid nametxt_folder is a name of a folder path
使用此代码,结果只能在网格中两次看到第一个文件
With this code , the result , i can see only the first file twice in grid
推荐答案
您的f出现了问题或每个循环:
CStr()将值转换为字符串。
因此,您的for循环遍历files数组中文件数字符串中的每个char。
因此将其更改为:
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
然后a将是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)
所以这应该起作用:
Dim files() As String = Directory.GetFiles(txt_folder.Text)
For Each a In files
folder_hard.Rows.Add(a)
Next
这篇关于如何列出硬盘驱动器中某个文件夹下的所有文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!