我无法复制我的文件,因为“不支持给定路径的格式”。

时间:2016-01-20 02:30:36

标签: vb.net

所以我正在尝试将用户放入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

请记住,我从未真正使用过listboxes,这是我第一次尝试使用CopyFile方法。有人能帮助我吗?

1 个答案:

答案 0 :(得分: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

相关问题