将目录中的文件名作为列表框中的项目获取 [英] Getting filename in a directory as items in list box

查看:72
本文介绍了将目录中的文件名作为列表框中的项目获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 

提前感谢

推荐答案

您好AbhinanNandu,

Hi AbhinanNandu,

根据您的描述,您只想在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:

https://msdn.microsoft.com/en-us/library/system.io.path.getfilename%28v=vs.110% 29.aspx?f = 255& MSPPError = -2147217396

最好的问候,

Cherry


这篇关于将目录中的文件名作为列表框中的项目获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆