C
C#7mo ago
Hengrip

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...
No description
No description
No description
24 Replies
phaseshift
phaseshift7mo ago
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.
Hengrip
Hengrip7mo ago
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
phaseshift
phaseshift7mo ago
if its your code that opens the file, then showing that would help
´ve checked the processes and nothing else is using the image

how did you check?
Hengrip
Hengrip7mo ago
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
Hengrip
Hengrip7mo ago
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
phaseshift
phaseshift7mo ago
oh you load them iinto an Image you need to .Dispose() then
Hengrip
Hengrip7mo ago
Dispose throws another error telling me there's no name for that or smt like that
phaseshift
phaseshift7mo ago
show the code
Hengrip
Hengrip7mo ago
This is it
phaseshift
phaseshift7mo ago
where is Dispose
Hengrip
Hengrip7mo ago
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"
phaseshift
phaseshift7mo ago
tbh i would have expected that to load the data from the file and not keep the file open 🤷‍♂️
Hengrip
Hengrip7mo ago
Definition my bad
phaseshift
phaseshift7mo ago
show the code...
Hengrip
Hengrip7mo ago
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
phaseshift
phaseshift7mo ago
yeah, you need to call dispose on the image
Hengrip
Hengrip7mo ago
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; } } }
phaseshift
phaseshift7mo ago
the image is the only thing I can see that could be holding the file handle
Hengrip
Hengrip7mo ago
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
phaseshift
phaseshift7mo ago
pcbImagen.Image = Image.FromFile(rutasImages[0]); pcbImagen.Image is the image. call dispose on that
Hengrip
Hengrip7mo ago
In the delete function or the call function?
phaseshift
phaseshift7mo ago
just call it before deleting
Hengrip
Hengrip7mo ago
Makes sense Lemme try it Ayyyy that totally worked!!! Thanks man really appreciate it
Hengrip
Hengrip7mo ago
It's supposed to throw this message saying the images were succesfully deleted
No description