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