Antony Alston

Antony Alston

  • NA
  • 2
  • 1.8k

Delete file which was in Picture Box before loading another

Aug 3 2014 12:03 AM

I have picturebox and i trying write code when the users loads another image on picturebox the old file which was in picture to be deleted. I trying to find solution for for 24 hrs still i didn't able find fully working solution.

I came up with 1 partial solution and following is partial code

 profilePictureBox.Dispose(); // When I use This After The Function Executed                                  PictureBox Disappears But File Delete Successfully.  try                 {                     if (File.Exists(oldfilename))                     {                         File.Delete(oldfilename);                         profilePicPictureBox.Load(picPath);                     }                     else                     {                         MessageBox.Show("File Not Found");                     }                 }                 catch (Exception ex)                 {                     MessageBox.Show(ex.ToString());                 }  

"old filename" will contain previous filename to delete and "picPath" will contains new path of image user chosen.

and also when tried method like

 profilePictureBox.Image = null; 

i am getting below error

System.IO.IOExecption:The Process cannot access the file  'filepath' because it is being      used by another process.

Answers (1)