2016-02-08 31 views
1

我想寫一個代碼,用戶可以在其中添加文件到列表視圖。這些文件必須被移到用戶指定的位置。我無法工作的是添加到列表視圖的文件的文件路徑。下面是用於移動文件的代碼:將文件從列表視圖移動到vb.net中的新目錄

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 

因爲「項目」是不是爲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 
+0

當您添加ListViewItem的,把音響.FullPath放入listitem的標籤。在複製時稍後使用它。 –

回答

0

回答謝謝Andrew Mortimer的建議。

代碼的ListView: 昏暗的STR(2)作爲字符串 昏暗的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 

代碼在列表視圖複製項目:

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 
相關問題