Take the 2-minute tour ×

I want to save image into a folder and the path of the folder to the database.

I have done this with File.Copy(filepath) command but it is giving me error when a file with the same name already exists there.

Second thing in this command is that I have to provide a filename in it from which it is copying the file. If I am modifying a record and not the image then it is giving error that file source cannot be empty.

I have also tried Picture1.image.save(filename) but I have not found any command to overwrite the existing file.

Please help me by providing a simplest way to do all this.

share|improve this question
It sounds like this is a problem with your database design if it has a problem with duplicates. It shouldn't so either your design is wrong or the way you are accessing the database is wrong. –  Ramhound Sep 18 '12 at 15:44
add comment

4 Answers

There's an overload to the File.Copy() method that accepts a bool which will determine whether to overwrite any existing files with the same name.


share|improve this answer
Sir, i don't want to use File.Copy method as it is taking the file necessarily for saving the image. I have already written that when i want to modify the data but not the image then it is giving error. I want to take the arguments from Picture Box like "Picture1.image.save(filename)" and not from the OpenFileDialog Box. Thanks –  Yogender Siwach Sep 17 '12 at 10:17
I'm struggling to understand what you mean sorry. The File.Copy has no association with the OpenFileDialogBox - you can derive the destination and source parameters any way you like - they're only strings. Does this answer your question? Struggling to see what you're stuck on sorry. State clearly what you expect the user to be able to do (the exact steps) and at what point it's failing so far and I can help you more accurately. At the moment - it doesn't sound like, given your problems, you need to avoid File.Copy - but like I say I don't really understand. –  DeeMac Sep 17 '12 at 10:46
add comment

File.Copy(sourceFileName, destFileName, true) Will force an overwrite of existing file.

Refer MSDN File.Copy

share|improve this answer
add comment

File.Copy(sourceFileName, destinationFileName);

sourceFileName shoudl be the full path of the source file(including the file name). destinationFileName should be the fullpath (including the filename) where you want to save the file.

share|improve this answer
add comment

you have to first check whether file exists or not?

using FileInfo,

FileInfo file = new FileInfo(location);
File.Copy(srcLocation, location);

In this way you can avoid the error.

share|improve this answer
add comment

Your Answer


