Click here to Skip to main content
15,898,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to make this list view that shows images in a folder
<prelang="c#">
public void LoadData()
{

var imgList = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
myListView.ItemsSource = imgList;

}


The ListView template is an Image and a button to delete the Image
The click event of the button is handled as follows
C#
private void DeleteImg(object sender, RoutedEventArgs e)
        {
            
            string delImg = (string)(sender as Button).DataContext;
            File.Delete(delImg);
            LoadData();
        }


I get the following error when I try to delete an image

System.IO.IOException: 'The process cannot access the file 'xyz' because it is being used by another process.'

xyz is the file name obviously

I think it's because the image is open in ListView
Any ideas how i can close the file before deleting it

What I have tried:

I tried
myListView.ItemsSource = null;
myListView.Items.Clear();
also:
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Posted
Updated 21-May-20 6:56am

Well, you can't delete a file that's in-use (if indeed it is your program that has it locked, I'd check with something like Process Explorer - Windows Sysinternals | Microsoft Docs[^] to see what's using it .... )

ok, so, I see 2 options ...
1) you 'queue' the files you want to delete, maybe write their names to a text file, when your program exits, you spawn a quick program to read the text file's contents being the fully qualified image filenames, and delete them
2) I'm not sure how your program is loading the images, but I would generate a thumb-nail in-memory of an image, and use that in the listview, that way you're not locking the original 'master' on disk

It's interesting that as you've tried,
myListView.ItemsSource = null;
myListView.Items.Clear();
doesnt work, it was my first thought, clear the listview and ?then the lock would go away, but there's obviously something I cant remember about using listview/images
 
Share this answer
 
v2
Thank you Garth J Lancaster for the response;

I finally found out this solution after hours of search
C#
public void LoadData()
{

var imgList = Directory.GetFiles(directoryPath, "*.jpg", SearchOption.AllDirectories);
myListView.ItemsSource = imgList;
foreach (string f in imgList)
            {

                var bi = new BitmapImage();
                bi.BeginInit();
                bi.CacheOption = BitmapCacheOption.OnLoad;
                bi.UriSource = new Uri(f);
                imgLister.Add(bi);
                bi.EndInit();
                bi.Freeze();
            }
}


Freeze() methode Frees up memory, and allows file to be deleted
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900