在WPF DocumentViewer中顯示XPS文件並關閉DocumentViewer實例後,XPS文件被鎖定,我無法將其刪除。我需要釋放XPS文件上的鎖,以便我可以刪除它,使用相同的名稱編寫另一個,並可選擇在新的DocumentViewer實例中顯示該新的XPS文件。我需要在同一個應用程序實例中執行此操作,而無需關閉應用程序(這是打印預覽方案)。如何讓WPF的DocumentViewer在源XPS文檔上發佈其文件鎖定?
換句話說,我將如何獲得下面的代碼來運行,而不會在「File.Delete(tempXpsFile);」處引發異常聲明?
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();
關閉應用程序不會釋放文件鎖,在WPF DocumentViewer doesn't release the XPS file提及,但不是在這種情況下的一個選項。
這一個引起了我的注意,我有一個瀏覽器下載一個文檔,一旦它設置了文檔,即使在應用這些更改後它也會失敗。當我加載文檔時,我正在處理原始文檔,這會在我重新加載文檔時導致發佈失敗。 – 2011-05-10 13:27:07