简体   繁体   中英

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.

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 .

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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM