Wednesday, 14 August 2013

Can't delete file because my program still has it open

Can't delete file because my program still has it open

I have a program the grabs PDF files and merges them, then deletes the
original unmerged files to the recycle bin.
I've been reworking the program to take advantage of the Syncfusion PDF
tools, and it's been mostly going smooth, right up until I try to delete
the file.
The warning popup tells me that it is my program that has the file open,
but I'm not sure where it's still open at in the program or how to find
out.
here's the function:
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

No comments:

Post a Comment