在尝试删除图像文件时,我遇到了一个问题。
我总是收到一个错误消息,上面写着:IOExeption was unhandled。因为文件正在被另一个进程使用,所以访问被拒绝。
我不知道是哪个进程,并且不知道如何解决这个问题。
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);
}
谢谢, Vinzcent
感谢所有人的帮助。
我使用了这个方法,现在它运行得非常好。
你的进程正在使用文件,你需要将图像设置为空,使用类似于这样的代码:
var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());
pctrbFoto.Image = img;
img = null;
GC.Collect();