rur2641
rur2641

Reputation: 699

Launching a program with a file uri

I am following this tutorial: http://blog.pieeatingninjas.be/2016/02/06/displaying-pdf-files-in-a-uwp-app/ This code is not working with a uwp app:

Windows.System.LauncherOptions options = newWindows.System.LauncherOptions();
options.ContentType = "application/pdf";
string fileUrl = "file:///C:/Users/Name/Documents/FileName.pdf";
await Windows.System.Launcher.LaunchUriAsync(new Uri(fileUrl), options);

Thank you.

Upvotes: 0

Views: 1604

Answers (2)

AH.
AH.

Reputation: 3091

This is how I did:

  1. The PDF file is added to the Assets folder and its Build Action is set to "Content".
  2. The code below is used for showing the PDF file with the default app.

       StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\MyDoc.pdf");
       var result = await Launcher.LaunchFileAsync(file);
    

Upvotes: 0

Jay Zuo
Jay Zuo

Reputation: 15758

We can't use Launcher.LaunchUriAsync(Uri) method to launch a PDF file by specify such a file path.

Ref from Remarks:

You cannot use this method to launch a URI in the local zone. For example, apps cannot use the file:/// protocol to access files on the local computer. Instead, you must use the Storage APIs to access files.

So when using your code, LaunchUriAsync method will always return false, it won't work.

To launch a file in UWP apps, we can use Launcher.LaunchFileAsync methods.

Firstlly, we need get a Windows.Storage.StorageFile object for the file. The Documents folder is a special folder, we can add documentsLibrary capability in app manifest and then use KnownFolders.DocumentsLibrary to get the PDF file in it. Or use FileOpenPicker to get the file. For more info, please see File access permissions and Open files and folders with a picker.

After we get the file object, we can launch the file with s several different options. For more info, please see Launch the default app for a file.

Upvotes: 2

Related Questions