Search code examples
c#wpfxpsdocumentviewer

How to adjust path of XPS document for wpf Application to work on another PC?


I have a simple wpf application that contains one window with a document viewer that I use to show an XPS document, I wrote this and it worked.

 XpsDocument myDoc = new XpsDocument(@"C:\Users\Ahmad
 Elsayed\Desktop\testxpsdoc\xpsTest\xpsTest\Assets\Dokhna - Problems -
 Report.xps", FileAccess.Read);

 DV.Document = myDoc.GetFixedDocumentSequence();

the problem however is that when I publish the app and use on another pc I get this:

System.IO.DirectoryNotFoundException: Could not find a part of the path

I tried to use relative path like this:

XpsDocument myDoc = new XpsDocument(@"\Assets\Dokhna - Problems - Report.xps", FileAccess.Read);

but still the same, given that I set the built action to Content and copy if newer to the xps file.

also this is the solution explorer: Solution Explorer


Solution

  • You should get the base directory for your running application which you can append to the relative path to the XPS file. This should work even if you deploy the application as long as you have the XPS file in an Assets folder next to the executable.

    string xpsFilePath = Path.Combine(
        AppDomain.CurrentDomain.BaseDirectory,
        @"Assets\Dokhna - Problems - Report.xps");
    
    XpsDocument myDoc = new XpsDocument(xpsFilePath, FileAccess.Read);