Cannot delete an image file that is shown in a listview

Posted by Enrico on Stack Overflow See other posts from Stack Overflow or by Enrico
Published on 2010-04-21T19:43:40Z Indexed on 2010/04/21 19:53 UTC
Read the original article Hit count: 483

Filed under:
|
|
|
|

Hello all,

In my listview I show thumbnails of small images in a certain folder. I setup the listview as follows:

var imageList = new ImageList();
foreach (var fileInfo in dir.GetFiles())
{
    try
    {
        var image = Image.FromFile(fileInfo.FullName);
        imageList.Images.Add(image);
    }
    catch
    {
        Console.WriteLine("error");
    }
}

listView.View = View.LargeIcon;
imageList.ImageSize = new Size(64, 64);
listView.LargeImageList = imageList;

for (int j = 0; j < imageList.Images.Count; j++)
{
    var item = new ListViewItem {ImageIndex = j, Text = "blabla"};
    listView.Items.Add(item);
}

The user can rightclick on an image in the listview to remove it. I remove it from the listview and then I want to delete this image from the folder. Now I get the error that the file is in use. Of course this is logical since the imagelist is using the file.

I tried to first remove the image from the imagelist, but I keep on having the file lock.

Can somebody tell me what I am doing wrong?

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET