1

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.

1
  • 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. Commented Sep 18, 2012 at 15:44

4 Answers 4

3

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.

http://msdn.microsoft.com/en-us/library/9706cfs5.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

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
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.
0

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

Refer MSDN File.Copy

Comments

0
if(File.Exists(destinationFileName))
{
  File.Delete(destinationFileName);
}

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.

Comments

0

you have to first check whether file exists or not?

using FileInfo,

FileInfo file = new FileInfo(location);
if(file.Exists())
{
File.Delete(location);
File.Copy(srcLocation, location);
}

In this way you can avoid the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.