删除".txt"从带有vb.net的组合框中列出的项目中 [英] Remove the ".txt" from items listed in a combobox with vb.net

查看:50
本文介绍了删除".txt"从带有vb.net的组合框中列出的项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,可读取文件夹中的所有txt文件并显示它们.它有效,但我想显示不带".txt"的名称,有人可以帮助我摆脱.txt.

这是我正在使用的:

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:

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

推荐答案

您可以只使用string.Substring删除最后四个字符,但是有更好的方法:
You could just use string.Substring to remove the last four characters, but there is a better way: Path.GetFilenameWithoutExtension[^]


尝试一下:

从VB.NET中的文件名和路径获取或提取/修剪扩展名 [
Try this:

Get or Extract/Trim the extension from a filename and path in VB.NET[^]

hope it helps :)


使用System.Linq的解决方案:

The solution using System.Linq:

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


这篇关于删除".txt"从带有vb.net的组合框中列出的项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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