问题描述
hi
我试图将目录中的文件名作为列表框中的项目。我部分完成了我想要的事情
i was trying to get file name in a directory as items in the list box. i am partially done what i want
Dim fileNames = My.Computer.FileSystem.GetFiles(" D:\",FileIO.SearchOption.SearchTopLevelOnly," * .txt" )
      For Each fileName As String in fileNames
          ListBox1.Items.Add(fileName)
     下一步
Dim fileNames = My.Computer.FileSystem.GetFiles("D:\", FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
For Each fileName As String In fileNames
ListBox1.Items.Add(fileName)
Next
这是我用过的代码。
这就是我在表格中得到的。但实际上我想要的只是显示我想在那里显示的经典文件名。
and this is what i am getting in the form. but actually what i want is only to display the file name that is only classic i want display there.
我不想显示"d:\"或".txt"在那里显示
i dont want to show"d:\" or ".txt" to display there
我如何继续
how i can go ahead
提前感谢
推荐答案
根据您的描述,您只想在ListBox中显示文件名,像这样:
According to your description, you just want to display the file name in the ListBox, like this:
您可以使用Path 。 GetFileName方法获取文件名,如下所示:
You can use Path.GetFileName Method to get file name, like this:
Dim fileNames = My.Computer.FileSystem.GetFiles("D:\test\", FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
For Each fileName As String In fileNames
Dim result As String = Path.GetFileName(fileName)
ListBox1.Items.Add(result)
Next
有关Path.GetFileName的更多详细信息,请参阅:
More detailed info about Path.GetFileName, please refer to:
最好的问候,
Cherry
这篇关于将目录中的文件名作为列表框中的项目获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!