Anilkumar
Anilkumar

Reputation: 85

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

I have a an html data in a literal and i make a pdf file say scan.pdf from that html using iTextsharp .But when i try to delete the created pdf file using File.Delete() method,it shows an error like

D:\Hosting\filepath\scan.pdf' because it is being used by another process

How can i solve it?

this is the code for creating pdf file from html and send it to some mailid as 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();
    }

Upvotes: 2

Views: 1025

Answers (4)

Anilkumar
Anilkumar

Reputation: 85

actually i forgot to free the mail attachment object,and thats why i got the error.When i put this code attachment.Dispose(); the error got cleared.

Upvotes: 1

Peter
Peter

Reputation: 27944

After saving the file in Textsharp you still have the file in use, because you haven't cleaned all handles to the file. Your writer is probably not within a using block, see which other classes should be disposed by checking if they are disposable. If you find them, make sure you dispose them, so all handles will be released.

some sample code:

using (FileStream stream = new FileStream(temp_filename, FileMode.Create))
{
    iTextSharp.text.Document document = new iTextSharp.text.Document();
    PdfWriter writer = PdfWriter.GetInstance(document, stream);
    ...
}

//    Do the delete after using block

The delete must be outside your using block. If you still have the error check the code where you are using the file (send it to the client etc).

Upvotes: 1

Thewads
Thewads

Reputation: 5053

I have never used this Textsharp library before but I would guess you can solve this by just either creating the file in a using block:

using(PdfWriter MyWriter=PdfWriter.GetInstance(Document,Stream))
{
...
}

or by simply calling .dispose() on it:

MyWriter.Dispose()

If you could show your code it would help to debug the exact location of your problem.

Upvotes: 0

volpav
volpav

Reputation: 5128

You're probably not disposing resources after you produce the PDF file. Usually it's done by calling "Close" and/or "Dispose" methods on the corresponding objects.

If the object implements IDisposable, you can just use the using construct. This way resources will be freed once the execution passes the using block (this also guarantees that the resources are freed even if there's an exception inside the using block).

Upvotes: 1

Related Questions