将文件添加到列表框 [英] Add files to a listbox
本文介绍了将文件添加到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨.我想将文件添加到列表视图.我有三个列表视图,当我浏览文件时,基于文件的扩展名,系统会将其分类为相应的列表视图.我希望文件本身不只显示路径.这是我的代码. plz帮帮我.
非常感谢!
Hi.. I want to add files to a listview. i have three listview, when i will browse for a file, based on its extension the system will classify it in the corresponding listview. i want the file itself to appear not the path only. here is my code. plz help me out.
Thanks a lot!
Imports System.IO
Imports System.Collections.Specialized
Imports Microsoft.Win32
Public Class Form1
Dim LView1Path As String
Dim LView2Path As String
Dim LView3Path As String
Dim file_name As String
Dim extension As String
Dim MessageBox As String
Private Sub btnBrowseImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowseImage.Click
OpenFileDialog1.Filter = ""
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
txtImage1.Text = OpenFileDialog1.FileName
file_name = txtImage1.Text
Dim index As Integer = file_name.IndexOf(".")
Dim length As Integer = file_name.Length
extension = file_name.Substring(index)
If extension = ".avi" Then
LVVideo.Items.Add(file_name, 0) ''i have written the condition for only 1 listview to test
End If
Else
'' The User clicked the Cancel button
txtImage1.Text = ""
End If
End Sub
End Class
推荐答案
您有两种方法来实现
1.
You have two ways to do it
1.
dim FileNameOnly as string = io.path.getfilename(file_name)
进一步阅读: MSDN:Path.GetFileName方法 [ ^ ]
2.
Further reading : MSDN: Path.GetFileName method[^]
2.
dim file_info as new IO.FileInfo(file_name)
messagebox.show(file_info.Name)
进一步阅读: MSDN:FileInfo类 [ ^ ]
我个人更喜欢选项1,因为您不必为工作要求创建任何其他对象.
Further reading : MSDN: FileInfo Class[^]
personally I prefer option 1 as you dont have to create any additional objects for the job requirements.
这篇关于将文件添加到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文