我想从文件夹中删除我的图片。但它会抛出“ 它被另一个进程 使用。”
这是我的代码:
fuProductImage.SaveAs(fileFolderPathTemp + fuProductImage.FileName);
Bitmap orgImage = new Bitmap(fileFolderPathTemp + fuProductImage.FileName);
ResizeAndSaveImages(orgImage, fileFolderPathLarge, fuProductImage.FileName, 66, 66);
File.Delete(fileFolderPathTemp + fuProductImage.FileName);
如何删除此文件?
答案 0 :(得分:6)
根据要求,我的建议是将第二行和第三行放入using
block中,如:
fuProductImage.SaveAs(fileFolderPathTemp + fuProductImage.FileName);
using ( Bitmap orgImage =
new Bitmap(fileFolderPathTemp + fuProductImage.FileName) )
{
ResizeAndSaveImages(
orgImage,
fileFolderPathLarge,
fuProductImage.FileName,
66,
66);
}
File.Delete(fileFolderPathTemp + fuProductImage.FileName);
这应该有帮助的原因是using
确保orgImage
正确地disposed,从而在调用File.Delete()
之前释放内存并释放文件句柄功能