使用文件 uri 启动程序
Launching a program with a file uri
我正在学习本教程:
http://blog.pieeatingninjas.be/2016/02/06/displaying-pdf-files-in-a-uwp-app/
此代码不适用于 uwp 应用程序:
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);
谢谢。
我们无法使用 Launcher.LaunchUriAsync(Uri) method 通过指定这样的文件路径来启动 PDF 文件。
参考自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.
所以当使用你的代码时,LaunchUriAsync
方法总是returnfalse
,它不会起作用。
要在 UWP 应用程序中启动文件,我们可以使用 Launcher.LaunchFileAsync methods。
首先,我们需要得到一个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。
获取文件对象后,我们可以使用几个不同的选项启动文件。有关详细信息,请参阅 Launch the default app for a file。
我是这样做的:
- PDF 文件已添加到 Assets 文件夹,其构建操作设置为 "Content"。
以下代码用于显示带有默认应用程序的 PDF 文件。
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\MyDoc.pdf");
var result = await Launcher.LaunchFileAsync(file);
我正在学习本教程: http://blog.pieeatingninjas.be/2016/02/06/displaying-pdf-files-in-a-uwp-app/ 此代码不适用于 uwp 应用程序:
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);
谢谢。
我们无法使用 Launcher.LaunchUriAsync(Uri) method 通过指定这样的文件路径来启动 PDF 文件。
参考自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.
所以当使用你的代码时,LaunchUriAsync
方法总是returnfalse
,它不会起作用。
要在 UWP 应用程序中启动文件,我们可以使用 Launcher.LaunchFileAsync methods。
首先,我们需要得到一个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。
获取文件对象后,我们可以使用几个不同的选项启动文件。有关详细信息,请参阅 Launch the default app for a file。
我是这样做的:
- PDF 文件已添加到 Assets 文件夹,其构建操作设置为 "Content"。
以下代码用于显示带有默认应用程序的 PDF 文件。
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(@"Assets\MyDoc.pdf"); var result = await Launcher.LaunchFileAsync(file);