Click here to Skip to main content
15,902,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i will like to add file to my listviwe control without the full path showing thanks

my code is
and if i try to add more file to my listview it will duplicate
VB
Imports System.IO
Imports System.Text

Public Class Form1
    Dim objItem As ListViewItem
    Private playList As New ArrayList

    Public Sub SaveArrayList(ByVal path As String, ByVal arrayList As ArrayList)

        Try
            Dim writer As New StreamWriter(path, False, Encoding.Unicode)

            For Each s As String In arrayList
                writer.WriteLine(s.ToString)
            Next

            writer.Close()
        Catch ex As Exception
            Debug.Write(ex.ToString)
        End Try

    End Sub
    Public Function LoadArrayList(ByVal path As String) As ArrayList

        Try
            If Not File.Exists(path) Then
                Return New ArrayList
            End If

            Dim reader As New StreamReader(path, True), s As String, list As New ArrayList

            Do
                s = reader.ReadLine
                If s = vbNullString Then Exit Do 'you can use the .Peek method aswell...
                list.Add(s)
            Loop 'Until reader.Peek = -1

            reader.Close()

            Return list

        Catch ex As Exception
            Debug.Write(ex.ToString)
            Return New ArrayList
        End Try

    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            playList.AddRange(OpenFileDialog1.FileNames)
            SaveArrayList("C:\playlist.txt", playList)
            For Each File In playList
                ' objItem.myplaylist.Items.Add(File)
                objItem = myplaylist.Items.Add(File)
                objItem.SubItems.Add((New FileInfo(File).Length / 1000).ToString("f2") + " " + "KB")
            Next
        End If
    End Sub
End Class

pls i need any help u can render am gr8full
Posted
Updated 3-Jan-15 9:49am
v2

Look at this approach:

VB
Dim playList As New ArrayList
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Multiselect = true
Dim dr As DialogResult = OpenFileDialog1.ShowDialog()
If (dr = System.Windows.Forms.DialogResult.OK) Then
        playList.AddRange((From x in OpenFileDialog1.FileNames select Path.GetFileName(x)).ToArray())
        playList.Dump()
End If
 
Share this answer
 
Comments
Wolfsoftonline 3-Jan-15 16:40pm    
That approach did not work
Zoltán Zörgő 3-Jan-15 16:42pm    
Why? It does what you requested: adds file names without path.
Sergey Alexandrovich Kryukov 3-Jan-15 17:52pm    
5ed.
—SA
Thanks for the help but i solved it my self
VB
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
           playList.Clear()
           playList.AddRange(OpenFileDialog1.FileNames)
           'SaveArrayList("C:\playlist.txt", playList)
           For Each File In playList
               '        ' objItem.myplaylist.Items.Add(File)
               objItem = myplaylist.Items.Add(My.Computer.FileSystem.GetFileInfo(File).Name)
               '        objItem = myplaylist.Items.Add(File)
               objItem.SubItems.Add((New FileInfo(File).Length / 1000).ToString("f2") + " " + "KB")
           Next
       End If
 
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