How do I allow a user to open a PDF stored in my UWP App?

Generation Continuum 191 Reputation points
2020-04-25T02:56:52.61+00:00

I'm developing a UWP app and I want the user to be able to open a PDF stored in my app in Reader or a browser on the user's desktop.

I first used the following code-behind from an app bar button click:

            string filename1 = "ModelHouseAppStencils.pdf";
            System.Diagnostics.Process.Start(filename1);

I got the following exception:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

Then I used the code:

StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/ModelHouseAppStencils.pdf"));

I then got the following exception:

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll

What do I do to solve this? Thanks.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Generation Continuum 191 Reputation points
    2020-04-26T03:55:48.933+00:00

    The full path of my PDF file isn't exactly where you specified it should be. It's in this full path:

    <yourproject><yourproject>\bin\<architecture>\Debug\AppX\Assets\

    How do I get rid of the top folder in this path or redirect my project to look for the PDF file in the current full path?

    GenerationContinuum5900


1 additional answer

Sort by: Most helpful
  1. Daniele 1,996 Reputation points
    2020-04-25T09:50:00.013+00:00

    At first check the properties of ModelHouseAppStencils.pdf: set "Copy to Output Directory" to "Copy if newer" or "Copy always" or set "Build Action" to "Content"

    Then

    StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(System.IO.Path.Combine("Assets", "ModelHouseAppStencils.pdf"));
    await Windows.System.Launcher.LaunchFileAsync(file);
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.