The following C# code keeps the file locked, so DeleteFile fails:
String srcFile = @"D:\I\deleteme\TempFiles\Mem\XPS\1.xps";
Package package = Package.Open(srcFile, FileMode.Open, FileAccess.Read, FileShare.Read);
XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Normal, srcFile);
/* Calling this is what actually keeps the file open.
Commenting out the call to GetFixedDocumentSequence() does not cause lock */
FixedDocumentSequence fds = xpsDoc.GetFixedDocumentSequence();
xpsDoc.Close();
package.Close();
File.Delete(srcFile); /* This will throw an exception because the file is locked */
Very similar code, which opens the XpsDocument from a file instead of a package doesn't keep the file locked:
String srcFile = @"D:\I\deleteme\TempFiles\Mem\XPS\1.xps";
XpsDocument xpsDoc = new XpsDocument(srcFile, FileAccess.Read, CompressionOption.Normal);
/* If XpsDocument is opened from file instead of package,
GetFixedDocumentSequence() doesn't keep the file open */
FixedDocumentSequence fds = xpsDoc.GetFixedDocumentSequence();
xpsDoc.Close();
File.Delete(srcFile); /* This will throw an exception because the file is locked */
I have seen another post indicating that if you open the document from a file, you need to manually close the underlying package, but that doesn't seem to be the case for me.
I have put a project and the sample file here: https://drive.google.com/open?id=1GTCaxmcQUDpAoPHpsu_sC3_rK3p6Zv5z