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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16729630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:43:25  来源:igfitidea点击:

Combobox just file name (without extention or path) VB.Net

vb.netcomboboxpath

提问by ABANDOND ACOUNT

I got the following code which is nice and all however it returns the entire path as well as name

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

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

What I'm after is the file name only and preferably without its extention...

我所追求的只是文件名,最好没有扩展名......

UPDATE

更新

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

回答by Hans Passant

You'll need to use the Path class insidethe loop:

您需要在循环使用 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

回答by Andrey Gordeev

Change your code to:

将您的代码更改为:

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

回答by Arsh

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