我實施了幫助程序,用於顯示來自here的縮略圖。在縮略圖旁邊,有一條刪除鏈接,用於呼叫此控制器:無法刪除文件(jpeg)
// HTTP POST: /Photo/Delete/1
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id, string confirmButton)
{
var path = "~/Uploads/Photos/";
Photo photo = photoRepository.GetPhoto(id);
if (photo == null)
return View("NotFound");
FileInfo TheFile = new FileInfo(Server.MapPath(path + photo.PhotoID + ".jpg"));
if (TheFile.Exists)
{
photoRepository.Delete(photo);
photoRepository.Save();
TheFile.Delete();
}
else return View("NotFound");
return View();
}
如果我禁用顯示縮略圖,則會刪除該文件。否則它發送錯誤:
System.IO.IOException: The process cannot access the file 'C:\Documents and Settings\ilija\My Documents\Visual Studio 2008\Projects\CMS\CMS\Uploads\Photos\26.jpg' because it is being used by another process.
我也不知道我的文件刪除功能是否正確寫入。在網上搜索,我看到每個人都使用File.Delete(TheFile);
,我無法使用它並使用TheFile.Delete();
。當使用File.Delete(TheFile);
時,出現以下錯誤:
Error 1 'System.Web.Mvc.Controller.File(string, string, string)' is a 'method', which is not valid in the given context C:\Documents and Settings\ilija\My Documents\Visual Studio 2008\Projects\CMS\CMS\Controllers\PhotoController.cs 109 17 CMS
我在這裏丟失了什麼嗎?
我知道拇指生成器使用該文件,我想也許File.Delete()與它有什麼關係? – 2010-03-24 14:06:45
我不知道你的代碼的其餘部分,但我假設你的縮略圖gen以某種方式打開文件的句柄(例如,使用Bitmap類)。當您完成使用該文件來生成縮略圖時,您必須.close()位圖類(或任何正在使用該文件的類)才能刪除它。 – 2010-03-24 14:09:33
我到底要做什麼?在鏈接我張貼在我的問題有一個類位圖位圖=新的位圖(FilePath);但bitmap.Close()不起作用。 – 2010-03-24 14:21:50