Can not delete file using ASP .NET 2.0 from the images directory within the website

Can not delete file using ASP .NET 2.0 from the images directory within the website

am 31.03.2008 06:23:22 von Anjan Bhowmik

Hi,

I use a form to upload an image to a directory on my site
(~/Images/UserName/file1.jpg). Using Server.mapPath function i get the
Physical path and save the file using
FileUpload1.PostedFile.SaveAs(filePath) function. The file is saved fine.

I also keep an daabase entry to store the virtual path
(~/Images/UserName/file1.jpg) for that specific user entry. The problem is
when i try to use File.Delete(Server.MapPath("~/Images/UserName/file1.jpg"))
to delete the file i get this error

------------------------------------------------------------ -----
The process cannot access the file
'C:\inetpub\wwwroot\WebSite\Images\UserName\file1.jpg' because it is being
used by another process.
------------------------------------------------------------ -----

I can not get past the error. So , plz help. Thnx in advance.