Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 异常消息:进程无法访问该文件,因为另一进程正在使用该文件_Asp.net_Vb.net_Itextsharp - Fatal编程技术网

Asp.net 异常消息:进程无法访问该文件,因为另一进程正在使用该文件

Asp.net 异常消息:进程无法访问该文件,因为另一进程正在使用该文件,asp.net,vb.net,itextsharp,Asp.net,Vb.net,Itextsharp,我正在使用下面的代码生成pdf。然后它填充一个gridview,以便用户可以单击刚刚生成的PDF。我收到一条异常消息:异常类型:IOException 进程无法访问文件“\Server\PDFs\PE10091026 Rev.pdf” 因为它正被另一个进程使用 如果我生成另一个PDF,那么可以打开上一个 如果没有人打开文件,你知道是什么在锁住它吗 Dim Doc1 As New Document Dim path As String = "\\server\PDFs\" Dim myUnique

我正在使用下面的代码生成pdf。然后它填充一个gridview,以便用户可以单击刚刚生成的PDF。我收到一条异常消息:
异常类型:IOException

进程无法访问文件“\Server\PDFs\PE10091026 Rev.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()

您可能需要使用
FileStream.dispose()
或使用
using
语句来处理
FileStream
。当前,当您第二次尝试打开时,
FileStream
仍以创建模式打开

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

我看不出您在哪里使用pdfWrite对象。是否需要关闭和/或处置?另外,在PdfWrite的构造函数中,您创建了一个新的文件流。创建并打开文件的。该文件流是如何关闭的?我确实尝试了filestream.dispose,但它作为选项不可用。您需要将filestream存储在一个变量中,以便可以调用它的Close或dispose方法。我已经使用了Doc1.dispose,如上所示。我确实尝试了filestream.dispose,但它作为选项不可用。您已处理Doc1,但未处理pdfWrite。