【问题标题】:Exception message: The process cannot access the file because it is being used by another process异常消息:该进程无法访问该文件,因为它正被另一个进程使用
【发布时间】:2013-09-23 13:58:35
【问题描述】:

我正在使用下面的代码生成 pdf。然后它会填充一个网格视图,以便用户可以单击刚刚生成的 PDF。我收到一条异常消息:Exception type: IOException

进程无法访问文件'\Server\PDFs\PE10091026-Rev.pdf' 因为它正被另一个进程使用。

如果我生成另一个 PDF,则可以打开前一个 PDF。

如果没有人打开文件,知道是什么在锁上它吗?

Dim Doc1 As New Document
Dim path As String = "\\server\PDFs\"
Dim myUniqueFileName = String.Format("{0}.pdf", Session("FileName") & "-Rev")
Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, New FileStream(path & myUniqueFileName, FileMode.Create))

Doc1.Open()
Dim test As String
test = Session("PDF")
Dim PDFHeader As String
PDFHeader = Session("Header")
Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
image.ScalePercent(70.0F)
Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)

Doc1.Add(image)
Doc1.Add(New Paragraph(PDFHeader))
Doc1.Add(New Chunk(line1))
Doc1.Add(New Paragraph(test))
Doc1.Close()
Doc1.Dispose()

【问题讨论】:

  • 我看不到您在哪里使用 pdfWrite 对象。是否需要关闭和/或处置?此外,在 PdfWrite 的构造函数中,您将创建一个新的 FileStream。这将创建并打开文件。该文件流是如何关闭的?
  • 我确实尝试过 filestream.dispose 但它不能作为选项使用
  • 您需要将 FileStream 存储在一个变量中,以便您可以调用它的 Close 或 Dispose 方法。

标签: asp.net vb.net itextsharp


【解决方案1】:

您可能需要使用FileStream.Dispose() 或使用Using 语句来处理FileStream。目前FileStream 在您第二次尝试打开时仍以create 模式打开。

Using fs As New FileStream(Path & myUniqueFileName, FileMode.Create)

    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(Doc1, fs)
    Doc1.Open()

    Dim test As String
    test = Session("PDF")
    Dim PDFHeader As String
    PDFHeader = Session("Header")
    Dim imagepath As String = Server.MapPath(".") & "/images/Header.png"
    Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagepath)
    image.ScalePercent(70.0F)
    Dim line1 As New iTextSharp.text.pdf.draw.LineSeparator(0.0F, 100.0F, BaseColor.BLACK, Element.ALIGN_LEFT, 1)

    Doc1.Add(image)
    Doc1.Add(New Paragraph(PDFHeader))
    Doc1.Add(New Chunk(line1))
    Doc1.Add(New Paragraph(test))
    Doc1.Close()
    Doc1.Dispose()

End Using

【讨论】:

  • 我已经使用了 Doc1.Dispose,如上图所示。我确实尝试了 filestream.dispose 但它不能作为一个选项使用
  • 您处理了 Doc1,但没有处理 pdfWrite。
猜你喜欢
  • 2010-12-10
相关资源
最近更新 更多