2008-11-12 2 views
10

XPS 문서를 열고 표시하는 WPF 응용 프로그램에서 작업하고 있습니다. 응용 프로그램이 닫히면 응용 프로그램에서 정리 된 XPS 문서를 삭제해야합니다. 그러나 특정 XPS 문서를 열 때 응용 프로그램은 파일을 삭제하려고 할 때 파일이 계속 사용되고 있다는 예외를 throw합니다. 약간의 이상한 점은 특정 XPS 문서를 열 때와 첫 페이지를 넘었을 때만 발생하기 때문입니다. WPF DocumentViewer가 XPS 파일을 해제하지 않습니다.

I가 사용되는 코드의 일부를

은 아래와 같다 : XPS 문서 탐색에

DocumentViewer m_documentViewer = new DocumentViewer(); 
XpsDocument m_xpsDocument = new XpsDocument(xpsfilename, fileaccess); 
m_documentViewer.Document = m_xpsDocument.GetFixedDocumentSequence(); 
m_xpsDocument.Close(); 

: 다음 XPS 문서를 여는

DocumentViewer 객체를 폐쇄

m_documentViewer.FirstPage(); 
m_documentViewer.LastPage(); 
m_documentViewer.PreviousPage(); 
m_documentViewer.NextPage(); 

및 파일을 삭제하는 중 :

m_documentViewer.Document = null; 
m_documentViewer = null; 
File.Delete(xpsfilename); 

모두 매우 기본이며 테스트 한 다른 문서와도 작동합니다. 그러나 특정 XPS 문서의 경우 삭제할 파일이 아직 사용 중이라는 예외가 표시됩니다.

내 코드에 이상이 있습니까?

감사합니다.

답변

2

는 회원 xpsDocument를 확인한 다음() 그것에 가까운 호출하지 않습니다 :)

+0

안녕하세요? 방금 XpsDocument 개체를 열어두고 끝내는 동안 XpsDocument.Close() 만 호출하여 파일을 삭제할 수있었습니다. 감사합니다. –

+1

응용 프로그램을 닫아 잠금을 해제 할 수있는 경우에만 작동합니다. 애플리케이션이 열려있는 동안 잠금이 해제되어야하는 경우 아래 또는 내 대답을 볼 필요가 있습니다. http://stackoverflow.com/questions/1442607/how-do-i-get-wpfs-documentviewer-to-release-its -file-lock-on-the-source-xps-docu –

0

답장을 보내 주셔서 감사합니다!

약간 낮은 수준이지만 아이디어가 부족할 때 명심하겠습니다. 어쨌든, 나는 버그에 대해 조금 더 알아 냈습니다. 예외의 원인이되는 특정 문서에는 이미지가 삽입되어 있습니다. 이미지를 제거하면 예외가 발생하지 않습니다. 이것은 DocumentViewer 버그 일 수도 있지만 여전히 시도 중입니다 ...

0

아니, 아직까지 아무 것도 없습니다. null로

  1. 설정 모든 윈도우의 "청산"이벤트에서 파일을 삭제하기 전에 :

    그냥 내가 실패 다음과 같은 방법을 시도했습니다, 열거합니다. 여기에는 DocumentViewer.Document 속성과 DocumentViewer 개체가 포함됩니다.

  2. ShowDialog()를 사용하여 창을 열고 나중에 null로 설정합니다. 파일을 삭제하여 창을 여는 System.Windows.Application 개체의 "Exit"이벤트로 이동했습니다. 여전히 파일이 사용되고 있다는 예외를 던집니다.

DocumentViewer bug ???

6

뷰어에 할당 된 XpsDocument가 열리는 System.IO.Packaging.Package를 닫아야합니다. 또한 동일한 응용 프로그램 세션에서 동일한 파일을 다시 열려면 PackageStore에서 패키지를 제거해야합니다.

시도

var myXpsFile = @"c:\path\to\My XPS File.xps"; 
var myXpsDocument = new XpsDocument(myXpsFile); 
MyDocumentViewer.Document = myXpsDocument; 

//open MyDocumentViwer's Window and then close it 
//NOTE: at this point your DocumentViewer still has a lock on your XPS file 
//even if you Close() it 
//but we need to do something else instead 

//Get the Uri from which the system opened the XpsPackage and so your XpsDocument 
var myXpsUri = myXpsDocument.Uri; //should point to the same file as myXpsFile 

//Get the XpsPackage itself 
var theXpsPackage = System.IO.Packaging.PackageStore.GetPackage(myXpsUri); 

//THIS IS THE KEY!!!! close it and make it let go of it's file locks 
theXpsPackage.Close(); 

File.Delete(myXpsFile); //this should work now 

//if you don't remove the package from the PackageStore, you won't be able to 
//re-open the same file again later (due to System.IO.Packaging's Package store/caching 
//rather than because of any file locks) 
System.IO.Packaging.PackageStore.RemovePackage(myXpsUri); 

네, 당신은 아마 패키지로 XpsDocument를 열지 않았고, 심지어 일이 무엇인지 모를 수도 알 - 나주의 -하지만 .NET은 뒤에 "를"당신이 그것을했다 장면을 정리하고 그 자체를 정리하는 것을 잊어 버립니다.

관련 문제