Tôi gặp sự cố khi cố gắng xóa tệp hình ảnh. Tôi luôn nhận được thông báo lỗi: IOExeption không được xử lý. Acces bị từ chối vì tập tin đang được sử dụng bởi một quá trình khác.C# File.Delete, tệp đang được sử dụng bởi một quy trình khác
Tôi không biết quy trình nào có thể và cách giải quyết.
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); }
Cảm ơn, Vinzcent
Nhờ tất cả cho sự giúp đỡ.
tôi đã sử dụng này và nó hoạt động rất tốt tại
quá trình của bạn là một trong đó sử dụng tập tin, bạn cần phải thiết lập hình ảnh null sử dụng một cái gì đó như thế này:
var img = Image. FromFile (foto.SPath) .GetThumbnailImage (GetWitdth (photo.SPath, GetHeight (photo.SPath, 150)), GetfHeight (photo.SPath, 150), null, mới IntPtr());
pctrbFoto.Image = img;
img = null;
GC.Collect();
Nói chung, bạn nên gọi GC.Collect: http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx Vì Hình ảnh triển khai IDisposable, bạn nên gọi hàm img.Dispose() thay vào đó, hoặc (tốt hơn) sử dụng khối "đang sử dụng". –
yes u r right;) – Sadegh