Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get WPF's DocumentViewer to release its file lock on the source XPS Document?
    text
    copied!<p>After showing an XPS file in the WPF DocumentViewer, and closing the DocumentViewer instance, the XPS file is locked and I cannot delete it. I need to release the lock on the XPS file so I can delete it, write another one with the same name, and optionally display that new XPS file in a new DocumentViewer instance. I need to do this in the same app instance - without having to close the app (this is a Print Preview scenario).</p> <p>In other words, how would I get the following code to run without throwing an exception at the "File.Delete(tempXpsFile);" statement?</p> <pre><code>var tempXpsFile = @"c:\path\to\Temporary.xps"; var previewWindow = new Window(); var docViewer = new DocumentViewer(); previewWindow.Content = docViewer; GenerateXpsFile(tempXpsFile); var xpsDocument = new XpsDocument(tempXpsFile); previewWindow.ShowDialog(); File.Delete(tempXpsFile); //this will throw an exception due to a file lock on tempXpsFile GenerateXpsFile(tempXpsFile); //assume this generates a different file //otherwise the scenario doesn't make sense as we could just skip the above delete //and this statement and re-use the same file previewWindow = new Window(); docViewer = new DocumentViewer(); previewWindow.Content = docViewer; previewWindow.ShowDialog(); </code></pre> <p>Closing the app does release the file lock, as mentioned in <a href="https://stackoverflow.com/questions/283027/wpf-documentviewer-doesnt-release-the-xps-file">WPF DocumentViewer doesn&#39;t release the XPS file</a>, but that is not an option in this scenario.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload