Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a combobox that reads all the txt files in a folder and displays them. it works but i want to show the names without the ".txt" can someone help with how i would get rid of the .txt.

here is what i m using:

VB
Private Sub RedirectCAE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    CAE_ComboBox.Items.Clear()
    CAE_ComboBox.Text = Nothing

    Dim myfolder As DirectoryInfo = New DirectoryInfo("\\mcusquanfs01\NRFZHD\RDM_NRFK_WN7\SynergyLite\CAE_Server")
    Dim strFiles() As FileInfo = myfolder.GetFiles("*.txt")

    For Each myItem As FileInfo In strFiles
        CAE_ComboBox.Items.Add(myItem)
    Next

End Sub
Posted

You could just use string.Substring to remove the last four characters, but there is a better way: Path.GetFilenameWithoutExtension[^]
 
Share this answer
 
Comments
Zachary.shupp 6-Mar-12 15:26pm    
i see how this works but i m a little lost with how i would convert what i have to do it for all the files in the folder. If you could provide an example that would be great.
 
Share this answer
 
The solution using System.Linq:

strFiles.ToList().ForEach(i => CAE_ComboBox.Items.Add(Path.GetFileNameWithoutExtension(i.Name)));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900