无法将类型为system.string的对象强制转换为类型system.io.fileinfo

时间:2015-06-28 11:31:25

标签: vb.net visual-studio-2010 type-conversion

您好我试图使用我的列表框中的路径,但它出现了错误

Dim items As New ListBox

'Getting the path of the selected file on opendialog'
For Each filename As String In OpenFileDialog1.FileNames
    items.Items.Add(filename)
Next

For Each sourcepath As FileInfo In items.Items()
    sourcepath.CopyTo(path)
Next

1 个答案:

答案 0 :(得分:3)

您已将字符串放入列表框中,因此当您循环这些项目时,您将获得字符串。如果你想要FileInfo个对象,你必须从字符串中创建它们:

For Each sourcepath As String In items.Items
    Dim fileInfo = New FileInfo(sourcepath).CopyTo(path)
Next