-
May 27th, 2010, 03:20 PM
#1
Thread Starter
Lively Member
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!?
-
May 27th, 2010, 03:24 PM
#2
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?
-
May 28th, 2010, 03:59 AM
#3
Thread Starter
Lively Member
Re: Help with copying
It gives the following error:
-
May 28th, 2010, 04:09 AM
#4
New Member
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
-
May 28th, 2010, 04:18 AM
#5
Thread Starter
Lively Member
Re: Help with copying
Thanks for your code but it gives your error every time i want to copy it
-
May 28th, 2010, 04:27 AM
#6
Re: Help with copying
Originally Posted by stylo
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
-
May 28th, 2010, 04:32 AM
#7
Thread Starter
Lively Member
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
-
May 28th, 2010, 04:46 AM
#8
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.
-
May 28th, 2010, 10:20 AM
#9
Thread Starter
Lively Member
Re: Help with copying
How do I do such a thing?
-
May 28th, 2010, 10:38 AM
#10
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
-
May 29th, 2010, 04:14 AM
#11
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|