LabRat
LabRat

Reputation: 2014

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

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

Upvotes: 1

Views: 5528

Answers (3)

Arsh
Arsh

Reputation: 1

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

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941447

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

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

Upvotes: 3

Andrey Gordeev
Andrey Gordeev

Reputation: 32459

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

Upvotes: 1

Related Questions