I am using two windows forms
, first form will have multiple image files populated in datagridview
and on the second form all those images are placed in another datagridview
control, the second datagridview
control has a button as Delete Image
that will delete the file.
after i click the button exception occurs as file is being used. but i alread have disposed the datagridview
and also cleared its rows but still it is causing not to release the image. I already have tried to call GC.Collect();
Method but it didn't helped. my sample code is below.
On First form:
// Commenting the code above
if (validuser)
{
dgvImages.Rows.Clear();
dgvImages.Dispose();
GC.Collect();
var form = new NewForm();
form.Show();
this.Hide();
}
and on second form:
private void dgvImages_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
try
{
if (e.ColumnIndex == 1)
{
string selectedpath = dgvImages.Rows[e.RowIndex].Cells[2].Value.ToString(); // this cell contains the full path of the image
if (File.Exists(selectedpath))
{
dgvImages.Rows.Clear();
dgvImages.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
File.Delete(selectedpath);
LoadImages();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); // File is in use
}
}
EDIT: Here is how i am loading images
private void LoadImages()
{
if (Directory.Exists(path))
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files = dir.GetFiles();
foreach (var f in files)
{
if (f.Extension.ToLower() == ".jpg" || f.Extension.ToLower() == ".png" || f.Extension.ToLower() == ".bmp" || f.Extension.ToLower() == ".tiff")
{
dgvImages.Rows.Add((Image.FromFile(f.FullName)), "Delete Image" ,f.FullName);
}
}
}
}