Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 30-Jan-12 0:46am
v2
Comments
Rajesh Anuhya 30-Jan-12 6:47am    
Code Tags added
--RA
DiiR 30-Jan-12 6:48am    
Thnx..i ddnt know how to do that

1 solution

You have two ways to do it

1.

VB
dim FileNameOnly as string = io.path.getfilename(file_name)


Further reading : MSDN: Path.GetFileName method[^]

2.
VB
dim file_info as new IO.FileInfo(file_name)
messagebox.show(file_info.Name)


Further reading : MSDN: FileInfo Class[^]

personally I prefer option 1 as you dont have to create any additional objects for the job requirements.
 
Share this answer
 
Comments
DiiR 30-Jan-12 10:16am    
thnx..i used the 1st option.. but am still getting..when i click on the file name nothing happens.
Simon_Whale 30-Jan-12 10:23am    
post a code snippet when your adding them name to the list and we'll go from there
DiiR 30-Jan-12 10:36am    
Dim index As Integer = file_name.IndexOf(".")
' i used the indexOf to determine the extension

Dim length As Integer = file_name.Length



extension = file_name.Substring(index)

If extension = ".avi" Then
LVVideo.Items.Add(file_name, 0) 'this is where the file is added to the listview

End If
Simon_Whale 30-Jan-12 10:46am    
http://msdn.microsoft.com/en-us/library/beth2052%28v=vs.90%29.aspx this is a great reference

I would replace the code with the following as it does the same but without string manipulation.

if IO.Path.GetExtension(file_name) = ".avi" then
LVVideo.Items.Add(IO.Path.GetFilename(file_name))
end if

"IO.Path.GetExtension(file_name)" will return the extension of the filename
"IO.Path.GetFileName(file_name)" returns the filename with its extension
DiiR 30-Jan-12 11:33am    
wow! it works!
thnx..i finally got wot i wntd..

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