Vb.net Can';我不能删除文件,因为我的程序仍然打开它

Vb.net Can';我不能删除文件,因为我的程序仍然打开它,vb.net,file-io,syncfusion,Vb.net,File Io,Syncfusion,我有一个程序可以抓取PDF文件并合并它们,然后将原始的未合并文件删除到回收站 我一直在重新编写程序,以利用Syncfusion PDF工具,并且一直都很顺利,直到我尝试删除该文件为止 弹出的警告告诉我是我的程序打开了文件,但我不确定它在程序中的哪个位置仍然打开,也不知道如何找到 下面是函数: Function MergePDFSync(ByVal Path As String, ByVal SavePath As String, ByVal outFileName As String, ByVa

我有一个程序可以抓取PDF文件并合并它们,然后将原始的未合并文件删除到回收站

我一直在重新编写程序,以利用Syncfusion PDF工具,并且一直都很顺利,直到我尝试删除该文件为止

弹出的警告告诉我是我的程序打开了文件,但我不确定它在程序中的哪个位置仍然打开,也不知道如何找到

下面是函数:

Function MergePDFSync(ByVal Path As String, ByVal SavePath As String, ByVal outFileName As String, ByVal DeleteOriginal As Boolean) As String
    On Error GoTo sError

    Dim CreateDate As Date = Now
    Dim finalFileName As String = ""
    Dim dInfo As New DirectoryInfo(Path)

    If dInfo.GetFiles("*.pdf").Length > 0 Then
        Dim doc As New Syncfusion.Pdf.PdfDocument
        Dim ldoc As Syncfusion.Pdf.Parsing.PdfLoadedDocument
        Dim file As String

        For Each f As FileInfo In dInfo.GetFiles("*.pdf")
            ldoc = New Syncfusion.Pdf.Parsing.PdfLoadedDocument(f.OpenRead)
            Syncfusion.Pdf.PdfDocument.Merge(doc, ldoc)
            ldoc.Close()
            doc.DisposeOnClose(ldoc)
        Next
        ldoc = Nothing

        finalFileName = Format(CreateDate, "M-d-yy-HHmmss-") & outFileName

        doc.Save(Path & "\" & finalFileName)
        doc.Close()
        doc = Nothing

        dInfo = Nothing

        If DeleteOriginal Then ' delete origional files
            dInfo = New DirectoryInfo(Path)
            For Each f As FileInfo In dInfo.GetFiles("*.pdf") 'For i As Integer = 0 To strFiles.Length - 1 ' run through all the files in the directory
                Console.WriteLine("MergePDF2 10.1 : " & f.Name & " = " & finalFileName)
                If Not f.Name = finalFileName Then
                    Console.WriteLine("MergePDF2 10.2 : Delete " & f.FullName)
                    My.Computer.FileSystem.DeleteFile(f.FullName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.SendToRecycleBin)
                End If
            Next
        End If
    End If

    Return finalFileName
    Exit Function
sError:
    ReportError("MergePDF2 " & Path & " " & Err.Description)
    Console.WriteLine("MergePDF2 " & Path & " " & ErrorToString())
End Function

我知道他们有一个合并功能的问题,我不知道它是否修复或您使用的版本。您可能想要尝试合并的替代方案

Dim docTarget As PdfLoadedDocument = New PdfLoadedDocument(targetFile)
Dim docAppend As PdfLoadedDocument = New PdfLoadedDocument(appendFile)

docTarget.Append(docAppend)
docTarget.Save()
docTarget.Close(True)
docAppend.Close(True)

或者逐个添加页面

doc.Pages.Add(ldoc.Pages[i]);

我知道他们的合并功能有问题,我不确定是修复了还是使用了哪个版本。您可能想要尝试合并的替代方案

Dim docTarget As PdfLoadedDocument = New PdfLoadedDocument(targetFile)
Dim docAppend As PdfLoadedDocument = New PdfLoadedDocument(appendFile)

docTarget.Append(docAppend)
docTarget.Save()
docTarget.Close(True)
docAppend.Close(True)

或者逐个添加页面

doc.Pages.Add(ldoc.Pages[i]);

可能重复的问题和其他十几个问题(打开我提供的链接,查看右侧“相关”问题的长长列表)。请在这个网站上搜索你收到的错误消息,因为很有可能这个问题以前在这里被问过(和回答过),其中一个可以为你回答这个问题。祝你好运。谢谢你给我小费,肯,只是我先尝试了所有这些东西。问题显然与.Close如何处理Syncfusion PDFLoadedDocument有关。请参阅下面的_lotus的有用答案。可能重复的和其他十几个(打开我提供的链接,并查看右侧的“相关”问题的长列表)。请在这个网站上搜索你收到的错误消息,因为很有可能这个问题以前在这里被问过(和回答过),其中一个可以为你回答这个问题。祝你好运。谢谢你给我小费,肯,只是我先尝试了所有这些东西。问题显然与.Close如何处理Syncfusion PDFLoadedDocument有关。请参阅下面的_lotus的有用答案。我保留了我的代码,只是我将ldoc更改为.Close(True),并且它工作正常。谢天谢地,合并不是问题,只是流没有随着加载的文档一起关闭。好发现,+1!我保留了原样的代码,只是将ldoc改为.Close(True),代码运行正常。谢天谢地,合并不是问题,只是流没有随着加载的文档一起关闭。好发现,+1!