c# - How do I delete the images from a folder after they have been used as the source for a PictureBox? -


this question has answer here:

link previous question, gives background situation

i answered own question above programmatically changing images tempory folder attachments saved. caused new issue me when fixing own problem feel separate former.

as program closes, delete images temporary directory. since have got preview work fine upon clicking on different images. following error when trying close program (the deleting of images happens on event):

the process cannot access file 'c:\temp\digitalarchive\filename.jpg' because being used process.

so have attempted resolve clearing picture temp folder before hand via:

if (picattachpreview.image != null)         {             picattachpreview.image.dispose();             picattachpreview.refresh();         }          //runs through each file in temporary directory , removes them clear folder         foreach (string item in directory.getfiles(tempfolder))         {             file.delete(item);         } 

edit: feel should show image updated reference:

if (chkattachments.text.contains(".jpg"))         {             var selectedimage = chkattachments.text;             picattachpreview.image = image.fromfile(path.combine(tempfolder, selectedimage));         } 

image.fromfile keeps file in use. can use image.fromstream instead.

var file = @"d:\pic\1.jpg"; using (var s = new system.io.filestream(file, system.io.filemode.open)) {     picturebox1.image = image.fromstream(s); } system.io.file.delete(file); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -