I'm running a listview to show thumbnails of a directory full of images in a MS Visual Studio application.
I'm trying to delete an image by selecting the images and pressing a button but for some reason the image is locked and won't delete. I suspect the thumbnail existing maybe the cause but can't figure a way around it
private void MemberImagesDel_Click(object sender, EventArgs e)
{
if (MemberImages.SelectedItems.Count > 0)
{
if (MemberImages.SelectedItems[0].Text != null)
{
DialogResult dialogResult = MessageBox.Show("Are you Sure?", "Delete Image", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
string filePath = Environment.CurrentDirectory + "\\Database\\Members\\" + MemberId.Text + "\\Images\\";
string fileName = MemberImages.SelectedItems[0].Text.ToString();
//Refresh images panel
MemberImages.Items.Clear();
ImageList imgs = new ImageList();
System.IO.Directory.CreateDirectory(filePath);
foreach (string item in Directory.GetFiles(filePath))
{
if (item != filePath + fileName)
{
//Add image to imagelist
imgs.ImageSize = new Size(128, 128);
imgs.Images.Add(Bitmap.FromFile(item));
FileInfo fi = new FileInfo(item);
MemberImages.Items.Add(fi.Name, imgs.Images.Count - 1);
}
}
MemberImages.LargeImageList = imgs;
File.Delete(filePath + fileName);
}
}
}
}
Has anyone encountered this issue before?