By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Simple Copy Application

P: 6
I am writing a small window app. All it does is depending on which button the user clicks it copies a file from one dir to another. Both the files are on network shares. . I am getting

IO Exception was unhandled
the file already exists

I just want to copy the files no matter if they already exist or not. Below is my code. What can I add to make it just copy no matter what?

Expand|Select|Wrap|Line Numbers
  1.            private void button2_Click(object sender, EventArgs e)
  2.         {
  3.             string fileToCopy = "S:/coversheets/worker.htm";
  4.             string newLocation = "F:/rbcover.htm";
  5.             System.IO.File.Copy(fileToCopy, newLocation);
  6.         }
Dec 12 '08 #1
Share this Question
Share on Google+
3 Replies


Curtis Rutland
Expert 2.5K+
P: 3,256
First of all, please use [CODE] [/CODE] tags when you post your code. It makes it easier for us to read.

MOD.

Now on to your question:
File.Copy Method
Check out the second overload that takes a source, destination, and a bool value. Passing true will overwrite files of the same name.

In case you did not already know, the MSDN can help you with things like this. You can explore every object, and each object's properties/members/methods, and each method's overloads.

Also, if you are using Visual Studio, the IntelliSense popup can give you much of this information. Notice that when you start typing a method's parameters, the little popup window appears. It can show you what overloads a method has and what parameters they take.
Dec 12 '08 #2

P: 6
Thanks, and thanks for the info about MSDN. That is a big help.
Dec 12 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
Yeah, a little googling goes a long way. Try searching for the object you have questions about first.

For example, I found the page I just linked to you by searching for System.IO.File.Copy in google. First result. Sometimes it'll take more effort.

Have fun!
Dec 12 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.