Tengo un problema cuando intento eliminar un archivo de imagen. Siempre aparece un error que dice: IOExeption no se ha manejado. Acceso denegado porque el archivo está siendo usado por otro proceso.C# File.Delete, archivo que está siendo utilizado por otro proceso
No sé qué proceso podría ser y cómo solucionarlo.
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { Album album = GetAlbum(comboBox1.SelectedIndex); Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex); txtPhotoPath.Text = Directory.GetCurrentDirectory() + "\\" + photo.SPath; lblExtention.Text = photo.SExtention; txtPhotoTitle.Text = photo.STitle; pctrbFoto.Image = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()); } private void btnChangePhoto_Click(object sender, EventArgs e) { Album album = GetAlbum(comboBox1.SelectedIndex); Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex); File.Delete("Albums\\Images\\" + photo.STitle + foto.SExtention); photo.SExtention = lblExtention.Text; photo.STitle = txtPhotoTitel.Text; Photo.SPath = txtPath.Text; File.Copy(photo.SPath, "Albums\\Images\\" + photo.STitle + photo.SExtention); }
Gracias, Vinzcent
Gracias a todos por la ayuda.
He utilizado este y funciona muy bien ahora
su proceso es el que utiliza el archivo, es necesario configurar la imagen para anular el uso algo como esto:
var img = Imagen. FromFile (foto.SPath) .GetThumbnailImage (GetWitdth (foto.SPath, GetHeight (foto.SPath, 150)), GetfHeight (foto.SPath, 150), nulo, nuevo IntPtr());
pctrbFoto.Image = img;
img = null;
GC.Collect();
En general, es una mala idea llamar a GC.Collect: http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx Dado que la imagen implementa IDisposable, debe llamar a img.Dispose() en su lugar, o (preferiblemente) usar un bloque "using". –
sí u r derecho;) – Sadegh