无法删除文件,因为我的程序仍然打开它

时间:2013-08-14 20:45:20

标签: vb.net file-io syncfusion

我有一个程序可以抓取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

1 个答案:

答案 0 :(得分:1)

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

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)

http://www.syncfusion.com/support/forums/pdf-windows/79124

或逐页添加页面

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

http://www.syncfusion.com/support/kb/876/How%20to%20merge%20two%20PDF%20documents