copy files

web4

Member
Joined
Mar 28, 2009
Messages
16
Programming Experience
Beginner
hi there i am newbie in vb.net....umm i want to copy a file for example photo.jpg to another directory let's say photosdir.....how do i do that? thanks...i hope my question is clear...
 
Guys it won't work...can you tell me where's the fault of my program?
here's the code

Dim str As String = "..\..\..\picture"
Dim strs, path As String
path = Directory.Exists(str)
Try
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
strs = OpenFileDialog1.FileName
My.Computer.FileSystem.CopyFile(strs, path)
MsgBox("File Copied Successfully")
Else
MsgBox(Err.Description)
End If
Catch
MsgBox(Err.Description)
End Try

because if i copy the file the source file will become false or true i don't know why....pls help me..thanks
 
If you notice the parameters in the intellisense for CopyFile() you'll see that at minimum the first two parameters are strings, not a string and a boolean. Here's all of the parameters:
msdn said:
' Usage
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,overwrite)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI)
My.Computer.FileSystem.CopyFile(sourceFileName ,destinationFileName ,showUI ,onUserCancel)
Also, you may want to avoid using "..\..\..\picture" as a valid path. I say this because the working directory may change and a path like that would cause unexpected results.
 
Something like this should work.. Obviously you need to stick the error checking back in.

VB.NET:
        Dim DestinationPath As String = "C:\destination\"
        Dim SourceFile As String
        Dim DestinationFileName As String


        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            SourceFile = OpenFileDialog1.FileName
            DestinationFileName = OpenFileDialog1.SafeFileName


            My.Computer.FileSystem.CopyFile(SourceFile, DestinationPath & DestinationFileName)

        End If


You will notice that the destination for 'My.Computer.FileSystem.CopyFile' needs to have the full path with filename. So it is My.Computer.FileSystem.CopyFile("C:\source\file.txt","C:\destination\file.txt").

What was happening when you tried your original code?
 
Back
Top