【问题标题】:giving error while creating pdf.The process cannot access the file.because it is being used by another process创建pdf时出错。进程无法访问文件。因为它正在被另一个进程使用
【发布时间】:2015-08-22 11:33:15
【问题描述】:

我正在将网页 div 导出为 pdf 并将该邮件发送到 pdf。第一次它创建pdf并正确发送邮件。但下一次它给出错误说。文件在这一行被另一个进程使用了​​

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

我的 aspx.cs 代码是-

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);

【问题讨论】:

  • 您是否尝试将 new FileStream(strPath, FileMode.Create) 分配给单独的变量,然后在完成写入后对其调用 Close() 或 Dispose()?

标签: c# asp.net pdf itextsharp


【解决方案1】:

在下面的代码中,您可以为文件名添加一些时间戳,以便创建不同的文件。

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

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

【讨论】:

  • 如果我更改文件路径名,它会给出错误,不支持给定路径的格式。@Nitin Varpe
  • D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf ....这是它创建@Nitin Varpe的路径
  • 还是同样的错误。 D:\may seoapp development\seoapp June 6\\Temp\WeeklyReport of Testing project08-06-2015_11:03:24.pdf @Nitin Varpe
  • 现在是什么错误,正在使用的文件或路径格式不正确?
【解决方案2】:

发送邮件后

Msg.Attachments.Dispose();

【讨论】:

    【解决方案3】:

    我通过添加日期和时间更改了文件名。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2015-12-07
      • 2011-04-17
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多