Mayank Jani

Mayank Jani

  • 1.4k
  • 465
  • 20.3k

Error: the file is being used by another process.

Aug 8 2019 8:52 AM
Hiii members,
 
I am working on a WinApp in C#. In a WinForm, I have a ListView and a PictureBox. When the form gets load, the ListView fetches all the pictures from a perticular folder and shows them as thumbnail.
When I click on any thumbnail, the selected image loads into the PictureBox. Now, there is a 'Delete' button, when I click the button to delete the selected image from the folder, it throws and error... 'it is being used by another process.'
If it is because using by the PictureBox then I have emptied the PictureBox but error remain, then I do not know about the ListView or what is the 'another process'
 
Please help.
 
Thank You.
Mayank Jani
 
 
My code...
 
var index = lvThumbs.SelectedItems[0].Index;                //This code fetches
pbImage.Image = Image.FromFile(_files[index].FullName);     //the image file location
pbImage.ImageLocation = _files[index].FullName;             //to delete the image file from a folder.

//MessageBox.Show("Deleting Path -> " + pbImage.ImageLocation); //This message box shows actual path and file name.
                               
pbImage.Image = null;

File.Delete(pbImage.ImageLocation);         //Here I got error, "it is being used by another process...".
               
MessageBox.Show("Deleted.");

Answers (5)