d0m1n1c February 2016

Move files from Listview to new directory in vb.net

I'm trying to write a code in which users can add files to a listview. These files must then be moved to a user specified location. What I can't get to work is the filepath of files that are added to the listview. Here's the code for moving the file:

sPath = My.Settings.DefaultPath & ComboBox1.Text & "\" & ComboBox2.Text & "\"
        If txtOnderwerp.Text = "" Then
            If ComboBox3.Text = "Make your choice..." Then
                MsgBox("Select subject!", MsgBoxStyle.Information)
            Else
                Try
                    For Each item As ListViewItem In ListView1.Items
                        My.Computer.FileSystem.CopyFile(item, sPath & ComboBox3.Text & "\" & item.Text, FileIO.UIOption.AllDialogs)
                        MsgBox("Copy succesfull.", MsgBoxStyle.Information)
                        ListView1.Items.Clear()
                        Me.Close()
                    Next
                Catch ex As Exception
                    MessageBox.Show("Error copying file: " & ex.Message)
                End Try
            End If
        End If

The code above states an error because 'item' is not a string for the FilsSystem.Copy command. Users can add files to a listview with this code:

 Using ofd As New OpenFileDialog
        ofd.Multiselect = True
        If ofd.ShowDialog = DialogResult.OK Then
            For Each fn As String In ofd.FileNames
                Dim fi As New IO.FileInfo(fn)
                Dim icons As Icon = SystemIcons.WinLogo
                Dim li As New ListViewItem(fi.Name, 1)
                If Not (ImageList1.Images.ContainsKey(fi.Extension)) Then
                    icons = System.Drawing.Icon.ExtractAssociatedIcon(fi.FullName)
                    ImageList1.Images.Add(fi.Extension, icons)
                End If
                icons = Icon.ExtractAssociatedIcon(fi.FullName)
                ImageList1.Images.Add(icons)
                    

Answers


d0m1n1c February 2016

Answer thanks to advice from Andrew Mortimer.

Code for ListView: Dim str(2) As String Dim itm As ListViewItem

    Using ofd As New OpenFileDialog
        ofd.Multiselect = True
        If ofd.ShowDialog = DialogResult.OK Then
            For Each fn As String In ofd.FileNames
                Dim fi As New IO.FileInfo(fn)
                Dim icons As Icon = SystemIcons.WinLogo
                'Dim li As New ListViewItem(fi.Name, 1)
                If Not (ImageList1.Images.ContainsKey(fi.Extension)) Then
                    icons = System.Drawing.Icon.ExtractAssociatedIcon(fi.FullName)
                    ImageList1.Images.Add(fi.Extension, icons)
                End If
                str(0) = fi.Name
                str(1) = fi.FullName
                icons = Icon.ExtractAssociatedIcon(fi.FullName)
                ImageList1.Images.Add(icons)
                itm = New ListViewItem(str)
                ListView1.Items.Add(itm)
            Next
        End If
    End Using

Code for copying items in listview:

Dim str As String
Dim copyfilename As String
Dim NewDir As String
Try
                    NewDir = My.Settings.DefaultPath & ComboBox1.Text & "\" & ComboBox2.Text & "\" & txtOnderwerp.Text
                    My.Computer.FileSystem.CreateDirectory(NewDir)

                    For Each item As ListViewItem In ListView1.Items
                        copyfilename = item.Text
                        str = item.SubItems.Item(1).Text
                        My.Computer.FileSystem.CopyFile(str, NewDir & "\" & copyfilename)
                        MsgBox("Kopiƫren is gelukt.", MsgBoxStyle.Information)
                        ListView1.Items.Clear()
                        Me.Close()
                    Next
                Catch ex As Exception
                    MessageBox.Show("Error: " & ex.Message)
                End Try

Post Status

Asked in February 2016
Viewed 2,044 times
Voted 7
Answered 1 times

Search




Leave an answer