user4762012
user4762012

Reputation:

giving error while creating pdf.The process cannot access the file.because it is being used by another process

im exporting web page div into pdf and sending that mail to pdf. for first time its creating pdf and sending mail properly.but for next time its giving error saying . file is bieng used by a another process at this line

 PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));

my aspx.cs code is-

System.IO.StringWriter stringWrite = new System.IO.StringWriter();


                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                design.RenderControl(htmlWrite);
                string myText = stringWrite.ToString().Replace("&", "&");
                StringReader sr = new StringReader(myText.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

                string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + ".pdf";
                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create));
                pdfDoc.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);

                pdfDoc.Close();
                pdfDoc.Dispose();

                LblNoteMsg.Text = strPath;
                DateTime input = DateTime.Now;
                int delta = DayOfWeek.Monday - input.DayOfWeek;
                DateTime dats = DateTime.Now.AddDays(delta);
                //this week
                DateTime monday = input.AddDays(delta);
                string MonDate = monday.ToShortDateString();
                DateTime sat = monday.AddDays(5);
                string SatDate = sat.ToShortDateString();
                StreamReader r = new StreamReader(Server.MapPath("~/WeeklyMail.txt"));
                string body = r.ReadToEnd();
                MailMessage Msg = new MailMessage();
                string MailId = txtMailId.Text;
                foreach (string ss in MailId.Split(",".ToCharArray()))
                {
                    if (string.IsNullOrEmpty(ss) == false)
                    {
                        Msg.To.Add(new MailAddress(ss));
                    }
                }


                Msg.Subject = "Weekly status Report";
                Msg.Body = body;
                Msg.IsBodyHtml = true;
                Msg.Attachments.Add(new Attachment(strPath));

                SmtpClient MailServer = new SmtpClient();
                try
                {

                    MailServer.Send(Msg);

Upvotes: 0

Views: 1731

Answers (3)

user4762012
user4762012

Reputation:

I have changed file name by adding date and time.

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + DateTime.Now.ToString(" dd-MM-yyyy_HH-mm-ss") + ".pdf";

Upvotes: 0

Nuno Silva
Nuno Silva

Reputation: 1

After sending the email

Msg.Attachments.Dispose();

Upvotes: 0

Nitin Varpe
Nitin Varpe

Reputation: 10694

In below code you can add some timestamp to file name so that different files get created.

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + ".pdf";

To

string strPath = Request.PhysicalApplicationPath + "\\Temp\\WeeklyReport of " + Projname + DateTime.Now.ToString("dd-MM-yyyy_HH:mm:ss") +".pdf";

Upvotes: 3

Related Questions