Image.FromFile("path"); <-- this code will hold the file
Main Topics
Browse All TopicsHi all,
My application loads user selected images in Imagelist for thumbnail display in Listview...Here's the code that i use do that..
ImageList.Images.Add(image
LstVwImage.Items.Add(image
So far so good, When user selects any image from listview and selects Delete button, I want it to remove items from listview and imagelist and delete physical file corrosponding that path, here's the code I am using...
LstVwImage.Items.RemoveAt(
ImageList.Images.RemoveAt(
FileInfo fi = new FileInfo(deleteFile);
fi.Delete();
Unfortunately its not able to delete file, saying "File is being used by another process"...Its not permission error as I am able to delete any file if its not loaded in imagelist or listview...
Either I'm getting crazy or stupid error!!!
Pls throw some light on this!!
Regards
MaulikCE
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thanks @RomanPetrenko
Your solution worked great in a first shot...
I'd be glad if you clear my doubt, what if I'd dispose image from imagelist before actually deleting file with fileInfo object...Isn't it same as your code for creating temporary image??
ImageList.Images[deleteInd
ImageList.Images.RemoveAt(
FileInfo fi = new FileInfo(deleteFile);
fi.Delete();
Regards
MaulikCE
This code will not work since Dispose() doesn't really frees all resources but put the object in dispose queue of memory management mechanism. When it will collect garbage then file will be unlocked. The Garbage Collector could be called explicitly through GC object. But you should understand that clean up of the managed heap (that what GC does) is very heavy operation.
To make your code work you have to call GC.Collect after releasing all instances of image.
ImageList.Images[deleteInd
ImageList.Images.RemoveAt(
GC.Collect(); //<<---- this is not good idea since you broke normal lifecycle of memory management and also slows your application.
FileInfo fi = new FileInfo(deleteFile);
fi.Delete();
So i still recommend you to make temprorary copy of images.
Regards,
Roman
Business Accounts
Answer for Membership
by: RomanPetrenkoPosted on 2005-04-04 at 12:06:19ID: 13700690
The file remains locked until the Image object is disposed.
p")) ToLoad,xx) ;
use following sample
===
Image xx = null;
using (Image a = Image.FromFile(@"c:\bmp.bm
{
xx = new Bitmap(a);
}
if (xx != null)
{
FileInfo fi = new FileInfo(@"c:\bmp.bmp");
fi.Delete();
ImageList.Images.Add(image
}
===