将多个文件名添加到列表框,但不带扩展名? C#
我正在制作一个程序,将可执行文件添加到 C# 中的列表框。
我正在尝试将项目添加到不带 .exe 扩展名的列表框中。这是我之前的代码:
listBox1.Items.Add(openFiles.SafeFileNames.Replace(".exe",""));
它运行良好,但不支持多个文件。当代码在选择多个项目而不是一个后运行时,它会添加项目“System.String[]”(这不好!D:)
我可以获得一些帮助吗?我会尝试更好地解释这一点,我没有太多睡眠,所以我可能会有点漫无目的 -
我想同时将多个文件添加到我的列表框中,并将我的 openFileDialog 设置为 multiSelect = true ,但排除文件扩展名 (.exe) 与各个项目一起输入到列表框中。
如果这不能轻松完成,我将切换回单选。
I'm making a program that adds executable files to a listBox in C#.
I'm trying to add the items to the listBox without the .exe extension. This is the code I had previously:
listBox1.Items.Add(openFiles.SafeFileNames.Replace(".exe",""));
It worked fine, but it doesn't have support for multiple files. When the code runs after selecting multiple items rather than one, it adds the item "System.String[]" (Which isn't good! D:)
Can I get some help? I'll try to explain this a little better, I haven't had much sleep so I might be rambling a bit -
I want to add multiple files to my listBox at the same time, with my openFileDialog that is set to multiSelect = true, but excluding the file extensions (.exe) from being entered into the listBox along with the individual items.
If this can't be done easily, I'll just switch back to single-select.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 System.IO.Path.GetFileNameWithoutExtension(file) 方法。
编辑:
Use
System.IO.Path.GetFileNameWithoutExtension(file)
method.EDIT:
我认为您需要执行一个循环,从返回的数组中的每个文件名中删除“.exe”:
I think you'll need to do a loop, removing the ".exe" from each file name in the returned array:
使用 FileInfo 类。它具有带扩展名和不带扩展名的名称,以及整个目录名和文件名。 MSDN 文件信息
Use the FileInfo class. It has name with and without extensions, as well as with the entire directory name and filename as well. MSDN FileInfo