unable to delete file (jpeg)

Posted by ile on Stack Overflow See other posts from Stack Overflow or by ile
Published on 2010-03-24T13:56:01Z Indexed on 2010/03/24 14:03 UTC
Read the original article Hit count: 331

Filed under:
|
|

I implemented helper for showing thumbnails from here
Next to thumbnail, there is delete link which calls this controller

//
        // 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();
        }

If I disable showing thumbnails then the file is deleted. Otherwise it sends error:

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.

I also don't know if my file delete function is properly written. Searching on the net, I see everyone uses File.Delete(TheFile);, which i'm unable to use and I use TheFile.Delete();
For File.Delete(TheFile); i get following error:

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

Am I missing something here?

Thanks in advance

© Stack Overflow or respective owner

Related posts about c#

Related posts about file