简体   繁体   中英

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 ??????

Check the access for the Application Pool user on IIS. Then try again with this code below:

var file = Server.MapPath("F16client.pdf")
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);

This can be a permission or file attribute problem.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM