2016-01-20 102 views
0

所以我试图复制我的用户将成为ListBox中的文件,我似乎有一个问题,因为就行了,我尝试复制我得到这个错误的文件:我无法复制我的文件,因为“给定路径的格式不受支持。”

An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll Additional information: The given path's format is not supported. 

后我相信一些研究是因为我合并了字符串和东西,但我不确定,所以我想我可能会问这里。

如果有帮助,我验证了列表框项目是有效的文件路径。

这里是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim result As DialogResult = MessageBox.Show("Are you sure you want to finish the playlist?", "Finish Playlist- WikiFinder", MessageBoxButtons.YesNo) 
    If (result = DialogResult.Yes) Then 
     For Each Item In ListBox1.Items 
      My.Computer.FileSystem.CopyFile(Item.ToString(), MusicMenu.FolderBrowserDialog2.SelectedPath.ToString() & Item.ToString()) 
     Next 
    Else 
    End If 
End Sub 

记住我从来没有真正与列表框工作,这是我第一次尝试的CopyFile方法。有谁能够帮助我?

+1

MusicMenu.FolderBrowserDialog2.SelectedPath.ToString()Item.ToString的'结果()'显然是无效的。使用'Path.Combine'来创建路径,但我们不知道列表框项目是否代表有效的路径段 – Plutonix

+0

谢谢,这工作! – Klink45

回答

0

Path.Combine方法解决了我的问题。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim result As DialogResult = MessageBox.Show("Are you sure you want to finish the playlist?", "Finish Playlist- WikiFinder", MessageBoxButtons.YesNo) 
    If (result = DialogResult.Yes) Then 
     For Each Item In ListBox1.Items 
      Dim str As String = IO.Path.Combine(MusicMenu.FolderBrowserDialog2.SelectedPath.ToString(), Item.ToString()) 
      My.Computer.FileSystem.CopyFile(Item.ToString(), str) 
     Next 
    Else 
    End If 
End Sub 

我相信当你写下路径时你不能合并字符串,这就是我做错了。

要了解更多关于Path.Combine:https://msdn.microsoft.com/en-us/library/fyy7a5kt(v=vs.110).aspx

相关问题