c# - Getting error when trying to delete a file in asp.net -


i have html data in literal , make pdf file scan.pdf html using itextsharp .but when try delete created pdf file using file.delete() method,it shows error like

d:\hosting\filepath\scan.pdf' because being used process

how can solve it?

this code creating pdf file html , send mailid attachment

   string mailformat = searchdt.rows[0][1].tostring();             emalbody.append(mailformat);             emalbody.replace("[date]", datetime.today.tostring());             string emailbody = emalbody.tostring();             message.body = emalbody.tostring();             ***htmltopdf(emailbody, "scan.pdf");***             system.net.mail.attachment attachment;             attachment = new system.net.mail.attachment(server.mappath("scan.pdf"));             message.attachments.add(attachment);             message.isbodyhtml = true;             message.subject = "";             smtpclient.send(message);             file.delete("d://filepath//scan.pdf");     public void htmltopdf(string html, string filepath)    {    document document = new document();    pdfwriter.getinstance(document, new filestream("d:\\filepath\\scan.pdf",filemode.create));     document.open();      itextsharp.text.html.simpleparser.stylesheet styles = new itextsharp.text.html.simpleparser.stylesheet();     itextsharp.text.html.simpleparser.htmlworker hw = new itextsharp.text.html.simpleparser.htmlworker(document);     hw.parse(new stringreader(html));     document.close();     } 

you're not disposing resources after produce pdf file. it's done calling "close" and/or "dispose" methods on corresponding objects.

if object implements idisposable, can use using construct. way resources freed once execution passes using block (this guarantees resources freed if there's exception inside using block).


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -