Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB .net - System.IO.File.Copy

Status
Not open for further replies.
Nov 19, 2002
5
US
I have a form which has a dropdown box (comboDrives)that list the drives on the computer. Then a ListBox (lbFiles)that displays the files in the selected drive. I also have 2 radio buttons (rb8000, rb9000)for the user to select a folder to copy that file to. What I want to do is copy the file(s) they select to the folder they select.

I keep getting the error:
"An unhandled exception of type 'System.InvalidCastException' occurred in system.windows.forms.dll

Additional information: Cast from type 'FileInfo' to type 'String' is not valid."

My Code:
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim x
x = lbFiles.SelectedItem

If rb8000.Checked = True Then
System.IO.File.Copy(x, "C:\Program Files\CheckPrint\Troy_8000\*.*")
Else
If rb9000.Checked = True Then
System.IO.File.Copy(x, "C:\Program Files\CheckPrint\HP_9000\*.*")
Else
MsgBox("You need to select a folder.", MsgBoxStyle.OKOnly)
End If
End If


End Sub
 
Code:
Dim _file As String
        Dim sourcedir As String = "c:\addon\nero55\"

        For Each _file In System.IO.Directory.GetFiles(sourcedir)
            System.IO.File.Copy(_file, "c:\addon\temp\" & _file.Remove(0, sourcedir.Length), True)
        Next

try this system.io.file.copy only copies one file at a time

Christiaan Baes
Belgium
"What a wonderfull world" - Louis armstrong
 
Thanks for the response. I really appreciate the help.

When you use:

Dim sourcedir As String = "c:\addon\nero55\"

"c:\addon\nero55\" is a static path to copy from. My "from" location isn't static. My Path would be the drive selected in comboDrives and the file selected in lbFiles. ex. D:\test.pp1 or E:\sample.pp2

So the path and file will be different every time. I can't figure out how to get the variable file to copy to the selected folder. I just get the string error noted above. I'm pretty new at this and I can't figure out how to apply what you've written above to my situation. It's probably simple...it's just not clicking in my head yet.
 
I figured it out. I used:

Dim x
x = comboDrives.SelectedItem
Dim y
y = lbFiles.SelectedItem

Try
If rb8000.Checked = True Then
System.IO.File.Copy(x & y.ToString(), "C:\Program Files\CheckPrint\Troy_8000\print.txt", False)
lbFiles.ClearSelected()

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top