我在嘗試刪除圖像文件時出現問題。 我總是得到一個錯誤,說:IOExeption未處理。訪問被拒絕,因爲該文件正在被另一個進程使用。C#File.Delete,另一個進程正在使用的文件
我不知道可能是什麼過程以及如何解決它。
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
感謝所有的幫助。
我用這個和它工作得很好,現在
你的過程是使用文件中的一個,你需要設置圖像null,則使用這樣的事情:
變種IMG =圖像。 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();
我同意。但我沒有看到顯示程序正在使用的選項。 – TamusJRoyce 2011-05-20 15:40:35