Antoine Pelletier Antoine Pelletier - 1 year ago 58
C# Question

Can't release file in MVC on IIS

Here's the function i made :

if (Valide())

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;

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 Source

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 :


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.