Brady
Brady

Reputation: 395

VB.Net - My.Computer.FileSystem.CopyFile() Won't Let Me Copy a File to a Directory

Basically I'm writing a program what will allow the user to choose a file or directory to copy over a network based on a set of stations and then they type in the location to copy those file/directory to.

I'm getting close to finishing I think but I'm having an issue with the My.Computer.FileSystem.CopyFile() method. I'm checking beforehand to see if the user-selected item to copy is a file or directory but in the case that the user enters a directory for the file to be copied to I get an error telling me that "The given file path ends with a directory separator character.", even though it's the DESTINATION location that it's erroring out on. I need to be able to have it copy a file to a directory if a directory is specified without a filename.

I tried playing around with the Trim functions and copying the name to the end of the destination path but I'm having a hard time getting just the file name of the source file.

Any ideas?

Here's the code for my fileCopy function far-

        If (pushFileSelectCheckBox1.Enabled) Then
        For Each item As String In stations
            copyTo = Path.Combine(copyTo, stations([i].ToString))
            copyToLoc = copyTo.ToString
            copyToLoc = Path.Combine(copyTo, pushLocationBox1.ToString.Remove(0, 36))

            If Directory.Exists(pushFrom1) Then
                If (System.IO.Directory.Exists(copyToLoc)) Then
                    My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc, True)
                    LogOutput("Directory 1 copied.")
                Else
                    Directory.CreateDirectory(copyToLoc)
                    LogOutput("Directory created.")
                    My.Computer.FileSystem.CopyDirectory(pushFrom1, copyToLoc, True)
                    LogOutput("Directory 1 copied.")
                End If
            ElseIf File.Exists(pushFrom1) Then
                If (System.IO.Directory.Exists(copyToLoc)) Then
                    My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc, True)
                    LogOutput("File 1 copied.")
                Else
                    Directory.CreateDirectory(copyToLoc)
                    LogOutput("Directory created.")
                    My.Computer.FileSystem.CopyFile(pushFrom1, copyToLoc, True)
                    LogOutput("File 1 copied.")
                End If
            Else
                MsgBox("Chosen file, or whatever, is neither a file nor a directory. What did you do?!?!", MsgBoxStyle.Critical, "Umm....")
            End If
            i += 1
        Next
        i = 0
    End If

Thanks in advance.

Upvotes: 0

Views: 2059

Answers (1)

the_lotus
the_lotus

Reputation: 12748

Use Path.GetFileName to get the filename from the pushFrom variable and append it at the end of copyToLoc. The second parameter of CopyFile need to end with a filename, not just a path.

Upvotes: 2

Related Questions