This is a migrated thread and some comments may be shown as answers.

PdfFormatProvider.Export not releasing resources

2 Answers 92 Views
Visual Studio Extensions
This is a migrated thread and some comments may be shown as answers.
sandra
Top achievements
Rank 1
sandra asked on 30 Dec 2015, 07:36 PM

I have a form that loads an existing pdf into a radPdfViewer. The user then has the option to move this file to a new location, with a new file name and then delete the original file.

I am able to load and view the original pdf. I am also able to export this file to a new location with a new file name. The problem occurs when I attempt to delete the original file. I am receiving the error message "The process cannot access the file ... because it is being used by another process."  Obviously I am not disposing of something properly, but I am unsure of the correct steps to take.  Any help would be appreciated.

I am using Visual Studio 2015 and the following code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        RadPdfViewer1.LoadDocument(OriginalFileName)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim exportProvider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider
        Dim NewFile As RadFixedDocument = exportProvider.Import(System.IO.File.OpenRead(OriginalFileName))
        Dim out As System.IO.Stream = System.IO.File.OpenWrite(NewFileName)

        exportProvider.Export(NewFile, out)
        out.Flush()
        out.Dispose()
        out.Close()
        NewFile = Nothing
        exportProvider = Nothing
        RadPdfViewer1.UnloadDocument()
        System.IO.File.Delete(OriginalFileName)            ----> this is the line that gives the error
    End Sub

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 04 Jan 2016, 07:41 AM
Hello Sandra,

Thank you for writing.

You should close the file stream which is reading the file when the document is imported as well. For example:
Private Sub radButton1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles radButton1.Click
    radPdfViewer1.UnloadDocument()
    Using fs = System.IO.File.OpenRead(OriginalFileName)
        Dim exportProvider As New Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider()
        Dim NewFile As RadFixedDocument = exportProvider.Import(fs)
 
        Using out As System.IO.Stream = System.IO.File.OpenWrite(NewFileName)
            exportProvider.Export(NewFile, out)
        End Using
    End Using
    System.IO.File.Delete(OriginalFileName)
End Sub

Please let me know if there is something else I can help you with. 
 
Regards,
Dimitar
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
sandra
Top achievements
Rank 1
answered on 04 Jan 2016, 08:50 PM

That worked. Such a silly mistake...I guess sometimes it just takes a second set of eyes.

Thanks so much for your response.  

Tags
Visual Studio Extensions
Asked by
sandra
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
sandra
Top achievements
Rank 1
Share this question
or