【问题标题】:Can't delete file because my program still has it open无法删除文件,因为我的程序仍然打开
【发布时间】:2013-08-14 20:45:20
【问题描述】:

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

【问题讨论】:

  • C# File Exception: cannot access the file because it is being used by another process 和大约十几个其他的可能重复项(打开我提供的链接,并查看其右侧的“相关”问题的长列表)。请搜索此站点以查找您收到的错误消息,因为很有可能之前已在此处提出(并回答)了该问题,其中一个可以为您回答该问题。祝你好运。
  • 感谢 Ken 的提示,但我先尝试了所有这些东西。该问题显然与 .Close 在 Syncfusion PDFLoadedDocument 上的工作方式有关。请参阅下面 the_lotus 的有用答案。

标签: vb.net file-io syncfusion


【解决方案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

【讨论】:

  • 我保留了我的代码,只是我将 ldoc 更改为 .Close(True) 并且它有效。谢天谢地,合并不是问题,只是流没有与加载的文档一起关闭。很好的发现,+1!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多