Can't delete image cuz my own program is using it??
You may know this exception error: can't delete the file because another process is using it, can anyone help me with this? Nothing I've tried has worked, please let me know if I wasn't clear or need more code...
24 Replies
check what the other program using the image is
e.g. with system internal's process explorer
if you open the file in your own process, then just release it before trying to delete it.
Yeah I know that's the problem but I don't know how to solve it specifically, like with what code or commands
I´ve checked the processes and nothing else is using the image it's the code using it
if its your code that opens the file, then showing that would help
how did you check?
´ve checked the processes and nothing else is using the image
how did you check?
With process explorer
As for the code, the pictures above show the part of it that uses the image, but I guess the problem is I don't know how to "kill" them if that makes sense
Here's the full thing if you wish to check it but I know some people don't like opening code files so I just put the images above for refrence
oh you load them iinto an Image
you need to .Dispose() then
Dispose throws another error telling me there's no name for that or smt like that
show the code
This is it
where is Dispose
Oh that's the original without dispose as it threw me an error saying there wan't a name for it or smt like that
I don't quite remember the especific error
Let me see
"does not contain a definition for dispose"
tbh i would have expected that to load the data from the file and not keep the file open 🤷♂️
Definition my bad
show the code...
private void btnEliminar_Click(object sender, EventArgs e)
{
if(rutaCarpeta != String.Empty)
{
DialogResult mensaje = MessageBox.Show("¿Esta seguro de eliminar los " + rutasImages.Count.ToString() + " archivos? ", "ADVERTENCIA", MessageBoxButtons.OKCancel);
if (mensaje == DialogResult.OK)
{
foreach (var rutaImagen in rutasImages)
{
Dispose();
File.Delete(rutaImagen);
}
MessageBox.Show(rutasImages.Count.ToString() + " elementos eliminados", "ADVERTENCIA");
rutasImages.Clear();
}
} else { MessageBox.Show("No existen imagenes para eliminar...", "ADVERTENCIA"); }
}
This is the part that deletes the photo
When you press a button
yeah, you need to call dispose on the image
private void btnSeleccionarCarpeta_Click(object sender, EventArgs e){
FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { rutasImages.Clear(); pcbImagen.Image = null; btnAdelante.Visible = false; btnAtras.Visible = false; rutaCarpeta = fbd.SelectedPath; lblCarpeta.Text = rutaCarpeta; DirectoryInfo di = new DirectoryInfo(@rutaCarpeta); foreach (FileInfo fi in di.GetFiles()) { if (fi.Extension == formato && fi.CreationTime <= fecha) { string fiRuta = @rutaCarpeta + @"" + @fi.Name; rutasImages.Add(@fiRuta); } } di = null; if(rutasImages.Count > 0) { lblElementos.Text = rutasImages.Count.ToString() + " Elementos Antiguos"; btnAdelante.Visible = true; btnAtras.Visible = false; indicePcb = 0; pcbImagen.Image = Image.FromFile(rutasImages[0]); } else { MessageBox.Show("No existen elementos que coincidan con los parametros de la app", "ADVERTENCIA"); lblElementos.Text = "0 Elementos Antiguos"; pcbImagen.Image = null; btnAdelante.Visible = false; btnAtras.Visible = false; } } }
FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { rutasImages.Clear(); pcbImagen.Image = null; btnAdelante.Visible = false; btnAtras.Visible = false; rutaCarpeta = fbd.SelectedPath; lblCarpeta.Text = rutaCarpeta; DirectoryInfo di = new DirectoryInfo(@rutaCarpeta); foreach (FileInfo fi in di.GetFiles()) { if (fi.Extension == formato && fi.CreationTime <= fecha) { string fiRuta = @rutaCarpeta + @"" + @fi.Name; rutasImages.Add(@fiRuta); } } di = null; if(rutasImages.Count > 0) { lblElementos.Text = rutasImages.Count.ToString() + " Elementos Antiguos"; btnAdelante.Visible = true; btnAtras.Visible = false; indicePcb = 0; pcbImagen.Image = Image.FromFile(rutasImages[0]); } else { MessageBox.Show("No existen elementos que coincidan con los parametros de la app", "ADVERTENCIA"); lblElementos.Text = "0 Elementos Antiguos"; pcbImagen.Image = null; btnAdelante.Visible = false; btnAtras.Visible = false; } } }
the image is the only thing I can see that could be holding the file handle
This is the part that "calls" the image
How can I integrate dispose correctly in the code? I'm really sorry C# isn't my strength
pcbImagen.Image = Image.FromFile(rutasImages[0]);
pcbImagen.Image is the image. call dispose on thatIn the delete function or the call function?
just call it before deleting
Makes sense
Lemme try it
Ayyyy that totally worked!!!
Thanks man really appreciate it
It's supposed to throw this message saying the images were succesfully deleted