Results 1 to 11 of 11

Thread: Help with copying

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Help with copying

    Hi, I have made a program. You can copy a file, for instance: I have a file and I want to copy it elsewhere, in a program. I already found some code but it isn't perfect. I already have:

    Code:
    Dim Copy As String = "C:\"
    Code:
    Private Sub TextBox1_Click
            OpenFileDialog1.FileName = Nothing
            OpenFileDialog1.ShowDialog()
            If Not OpenFileDialog1.FileName = Nothing Then
                TextBox1.Text = OpenFileDialog1.FileName
            End If
            OpenFileDialog1.FileName = Nothing
    Code:
        Private Sub Button2_Click
            System.IO.File.Copy(TextBox1.Text, TextBox2.Text)
    But the problem is.... It doesn't work. I can open a file but the copy part won't work.. I am a absolute beginner

    Can someone help me please!?

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with copying

    What exactly do you have in your TextBox2.Text property? Also, when you say "it doesnt work" - what exactly happens? Do you get any error messages or does it just do nothing?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Re: Help with copying

    It gives the following error:


  4. #4
    New Member
    Join Date
    May 2010
    Posts
    9

    Re: Help with copying

    The Function ShowDialog returns whether the user canceled or not so you can use it as follows:
    Code:
    If OpenFileDialog1.ShowDialog() Then
        TextBox1.Text = OpenFileDialog1.FileName
    End If
    Then if your just letting the user type in the path to copy to you can put this to handle Button2_Click:
    Code:
    Try
        System.IO.File.Copy(Textbox1.Text,Textbox2.Text)
    Catch
        'Runs if the copying gets an error
        Msgbox("The Path to Copy To was not valid.")
    EndTry

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Re: Help with copying

    Thanks for your code but it gives your error every time i want to copy it

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with copying

    Quote Originally Posted by stylo View Post
    It gives the following error:

    Well then I'm guessing you have not put the file name in your TextBox2 box when you run the code... you should be entering the full path to the file (including the file extension), not just the path to the directory you want to copy it to. So for example you might enter C:\SomeFolder\myfile.txt
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Re: Help with copying

    Thanks for your reply. It works now but is there a way that it names the file the same as the input file? Because this is annoying

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with copying

    You can name it whatever you want... If you want the user of your application to just specify a directory rather than the file name as well then just combine what they enter in TextBox2 (which would just be the full path to the directory they want to copy the file to) with the file name you want. You can use IO.Path.Combine to combine both parts of the path.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Re: Help with copying

    How do I do such a thing?

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Help with copying

    I just explained - if you dont know how to use a particular method then look it up http://www.lmgtfy.com/?q=IO.Path.Combine
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    85

    Re: Help with copying

    I still don't get it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width