将文件从Listview移动到vb.net中的新目录

时间:2016-02-08 08:36:44

标签: vb.net listview

我试图编写一个用户可以将文件添加到列表视图的代码。然后必须将这些文件移动到用户指定的位置。我无法工作的是添加到listview的文件的文件路径。这是移动文件的代码:

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

上面的代码表示错误,因为' item'不是FilsSystem.Copy命令的字符串。用户可以使用以下代码将文件添加到列表视图中:

 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)
                ListView1.Items.Add(fi.Name, fi.Extension)
            Next
        End If
    End Using

1 个答案:

答案 0 :(得分:0)

感谢Andrew Mortimer的建议。

ListView的代码:     Dim str(2)As String         Dim itm作为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

在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