Bakhtiyar
Bakhtiyar

Reputation: 21

How to delete a file while the application is running

I have developed a C# application, in the application the users choose a photo for each record. However the user should also be able to change the pre-selected photo with a newer one. When the user changes the photo the application first deletes the old photo from the application directory then copies the new photo, but when it does that the application gives an exception because the file is used by the application so it cannot be deleted while the application is running. Does any one have a clue how to sort this out? I appreciate your help

This is the exception

The process cannot access the file 'D:\My Projects\Hawkar'sProject\Software\Application\bin\Debug\Photos\John Smith.png' because it is being used by another process.

 //defining a string where contains the file source path
            string fileSource = Open.FileName;

            //defining a string where it contains the file name
            string fileName =  personNameTextBox.Text + ".png" ;

            //defining a string which specifies the directory of the destination file
            string fileDest = dir + @"\Photos\" + fileName;

            if (File.Exists(fileDest))
            {

               File.Delete(fileDest);
               //this is a picturebox for showing the images
               pbxPersonal.Image = Image.FromFile(dir + @"\Photos\" + "No Image.gif");
               File.Copy(fileSource, fileDest);
            }
            else
            {
                File.Copy(fileSource, fileDest);

            }
            imageIDTextBox.Text = fileDest;

Upvotes: 2

Views: 1362

Answers (2)

Bakhtiyar
Bakhtiyar

Reputation: 21

Thanks a lot for your help and sorry for the mistake in the code I just saw it, my original code is just like as you have written but I don't know maybe when I posted accidentally I've put like this. when the application runs there is a picturebox which the image of each record is shown, that is why the application is giving an exception when I want to change the picture because it has been used once by the picturebox , however I've also tried to load another picture to the picture box before deleting the original one but still the same. I've modified the above could if you want to examine it

Upvotes: 0

EvilMM
EvilMM

Reputation: 901

First of all, you code is not good. The new image is only copied if there is currently no image (else). But if there is an old image, you only delete this image, but never copy the newer one (if).

The code should better look like this:

if (File.Exists(fileDest))
{
    File.Delete(fileDest);
}

File.Copy(fileSource, fileDest);
imageIDTextBox.Text = fileDest;

This code should work, but if you are getting an exception that the file is already in use, you should check "where" you use the file. Perhaps you are reading the file at program start. Check all parts of your program you are accessing these user files if there are some handles open.

Upvotes: 3

Related Questions