C# 创建pdf时出错。该进程无法访问该文件。因为其他进程正在使用该文件

C# 创建pdf时出错。该进程无法访问该文件。因为其他进程正在使用该文件,c#,asp.net,pdf,itextsharp,C#,Asp.net,Pdf,Itextsharp,我正在将网页div导出为pdf格式,并将该邮件发送到pdf格式。这是它第一次正确地创建pdf和发送邮件,但下一次它给出了错误的说法。文件正在被此行的另一个进程使用 PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strPath, FileMode.Create)); 我的aspx.cs代码是- System.IO.StringWriter stringWrite = new System.IO.StringWri

我正在将网页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);

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

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


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

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


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

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


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

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

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

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

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

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

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

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

发送电子邮件后

Msg.Attachments.Dispose();

发送电子邮件后

Msg.Attachments.Dispose();

发送电子邮件后

Msg.Attachments.Dispose();

发送电子邮件后

Msg.Attachments.Dispose();


您是否尝试将新FileStream(strPath,FileMode.Create)分配给一个单独的变量,然后在完成编写后对其调用Close()或Dispose()?是否尝试将新FileStream(strPath,FileMode.Create)分配给一个单独的变量,然后调用Close()或Dispose()您是否尝试将新的FileStream(strPath,FileMode.Create)分配给一个单独的变量,然后在您完成编写时对其调用Close()或Dispose()?您是否尝试将新的FileStream(strPath,FileMode.Create)分配给一个单独的变量,然后调用Close()或Dispose()当您完成写入时,它的给定错误?如果我更改文件路径名,则给定路径的格式不受支持。@Nitin VarpeUse path.Combine和D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf…这是它创建@Nitin Varpestill的路径。同样的错误。D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015\u 11:03:24.pdf@Nitin VarpeWhats错误,正在使用的文件或路径格式不正确?如果更改文件路径名,则给出的路径格式不受支持。@Nitin VarpeUse path.Combine,和D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf…这是创建@Nitin Varpestill相同错误的路径。D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015\u 11:03:24.pdf@Nitin VarpeWhats错误,正在使用的文件或路径格式不正确?如果更改文件路径名,则给出的路径格式不受支持。@Nitin VarpeUse path.Combine,和D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf…这是创建@Nitin Varpestill相同错误的路径。D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015\u 11:03:24.pdf@Nitin VarpeWhats错误,正在使用的文件或路径格式不正确?如果更改文件路径名,则给出的路径格式不受支持。@Nitin VarpeUse path.Combine,和D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015 10:58:39.pdf…这是创建@Nitin Varpestill相同错误的路径。D:\may seoapp development\seoapp june 6\\Temp\WeeklyReport of Testing project08-06-2015\u 11:03:24.pdf@Nitin VarpeWhats现在有错误,正在使用文件或路径格式不正确?