Click here to Skip to main content
16,004,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to move one picture file to another file. The user can give the image name by using a save file dialog box. It works properly for non exists files. But when give an exists name, then ask if you want to replace a file from a message box. Then I select "OK" as the response. But Image didn't move correctly. What's the reason?? I can't imagine.

C#
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                    Saved_File = saveFileDialog1.FileName;
                    imageName = Saved_File;

                    if (pbMergeImagePreview.Image != null)
                        pbMergeImagePreview.Image.Dispose();

                    pbMergeImagePreview.InitialImage = null;

                    if (File.Exists(tempName) && !File.Exists(imageName))
                    {
                       
                        File.Move(tempName, imageName);
                      
                       

                    }
                    else if (File.Exists(tempName) && File.Exists(imageName))
                    {
                        File.Move(tempName, imageName);
                        
                    }

                }
Posted
Updated 21-Aug-11 18:14pm
v2

According to the File.Move documentation[^] an IOException is thrown when the distination file already exists.

You can use File.Copy(source, destination, true)[^] to copy the file to the distination and overwrite any existing file. and then use File.Delete[^] to delete the source/original file

Edit: your if-else-if can be simplified
C#
if (File.Exists(tempName) && !File.Exists(imageName))
{
    File.Move(tempName, imageName);
}
else if (File.Exists(tempName) && File.Exists(imageName))
{
    File.Move(tempName, imageName);
}


==

C#
if (File.Exists(tempName)
    File.Move(tempName, imageName);// this is where  I suggest you do your File.Copy and File.Delete
 
Share this answer
 
v3
If you refer MSDN for File.Move method, it says Move method works across disk volumes, and it does not throw an exception if the source and destination are the same.

If you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.

Hope, it gives the answer/reason to your question.
 
Share this answer
 
In fact, if you catch exception in your code, it will give the clear picture to the problem.

C#
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
                {
                try
                {
                    Saved_File = saveFileDialog1.FileName;
                    imageName = Saved_File;

                    if (pbMergeImagePreview.Image != null)
                        pbMergeImagePreview.Image.Dispose();

                    pbMergeImagePreview.InitialImage = null;

                    if (File.Exists(tempName) && !File.Exists(imageName))
                    {

                        File.Move(tempName, imageName);



                    }
                    else if (File.Exists(tempName) && File.Exists(imageName))
                    {
                        File.Move(tempName, imageName);

                    }
                }
                catch (IOException ex)
                {
                    Console.Writeline("Error here: " + ex.Message);
                }
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900