Combobox只是文件名(没有扩展或路径)VB.Net

时间:2013-05-24 07:20:25

标签: vb.net combobox path

我得到了以下代码,这是很好的但是它返回整个路径以及名称

        For Each s As String In System.IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC\BASIS\")
        combobox1.Items.Add(s)
    Next

我所追求的只是文件名,最好没有它的延伸......

更新

            For Each s As String In GetFileNameWithoutExtension("C:\VTS\TREADSTONE LT\ATC\BASIS\")
        combobox1.Items.Add(s)
    Next

3 个答案:

答案 0 :(得分:3)

您需要在循环中使用Path类

Dim dir = "C:\VTS\TREADSTONE LT\ATC\BASIS\"
For Each file As String In System.IO.Directory.GetFiles(dir)
    combobox1.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next

答案 1 :(得分:1)

将您的代码更改为:

For Each s As String In System.IO.Directory.GetFiles("C:\VTS\TREADSTONE LT\ATC\BASIS\")
    s = s.Substring(0,s.LastIndexOf("."))
    combobox1.Items.Add(s)
Next

答案 2 :(得分:0)

Dim di As New IO.DirectoryInfo(My.Application.Info.DirectoryPath + "\softbelldata")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
    If dra.Extension = ".mdb" Then
        txtyear.Items.Add(System.IO.Path.GetFileNameWithoutExtension(dra.Name))
    End If
Next