Antoine Pelletier Antoine Pelletier - 2 months ago 17
C# Question

Can't release file in asp.net MVC on IIS

Here's the function i made :

if (Valide())
{
try
{
File.Delete(Server.MapPath("F16client.pdf"));
}
catch
{
}

using (FileStream ms = new FileStream(Server.MapPath("F16client.pdf"), FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader lecteur = new PdfReader(Server.MapPath("~/Formulaires/f16.pdf"));

PdfStamper etampeur = new PdfStamper(lecteur, ms);

// DO STUFF, Whatever

etampeur.FormFlattening = true;
etampeur.Close();
lecteur.Close();

System.Net.Mail.Attachment at = new Attachment(Server.MapPath("F16client.pdf"));
}
}


It work when testing local, but on IIS, this W3W process won't let go of my f16client.pdf and i always end up with :


The process cannot access the file 'D:\inetpub\wwwroot\Formulaire16\F16client.pdf' because it is being used by another process.


Why is that file still in use when on IIS and not when testing local ??????

Answer

Well... no magic here. Thanks to Feryal Badili for pointing out the problem, an email attachement is something that actually holds the ressource...

So, a simple :

    at.Dispose();

before the end of the function and alls good. Yeah...

The reason why it worked on local is probably because i was pushing the stop everything button everytime i tested, and therefore, i was killing the handle everytime it tried my function.

Comments