Vinothkumar Arputharaj
Vinothkumar Arputharaj

Reputation: 4569

How to launch PDF file in associated app in HoloLens UWP?

I'm trying to open a PDF file, packaged inside app folder ("Assets"), in HoloLens (C#) app.

        string imageFile = @"Assets\test.jpg";

        var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);

        Debug.WriteLine("Opening file in path :" + file.Path);

        if (file != null)
        {
            Debug.WriteLine("File found\nLaunching file...");

            var options = new Windows.System.LauncherOptions();
            options.DisplayApplicationPicker = true;
            // Launch the retrieved file
            var success = await Windows.System.Launcher.LaunchFileAsync(file);

            if (success)
            {
                Debug.WriteLine("File launched successfully");
                // File launched
            }
            else
            {
                Debug.WriteLine("File launch failed");
                // File launch failed
            }
        }
        else
        {
            Debug.WriteLine("Could not find file");
            // Could not find file
        }

Getting Access is Denied error.

Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at SchneiderDemoLibrary.HoloHelper.<DefaultLaunch>d__3.MoveNext() Exception caught.

Can someone help me to solve this issue? I simply want to open some file with the corresponding app associated with the file type.

Upvotes: 1

Views: 2670

Answers (1)

BrainSlugs83
BrainSlugs83

Reputation: 6416

  1. Verify that the file is included in your project, and that the Build Action is set to "Content".

  2. Switch to retrieving the IStorageFile reference via the StorageFile.GetFileFromApplicationUriAsync call.

The following code is working for me in a regular UWP application:

private async void OpenPdfButton_Click(object sender, RoutedEventArgs e)
{
    var file = await StorageFile.GetFileFromApplicationUriAsync
    (
        new Uri("ms-appx:///Content/output.pdf")
    );

    await Launcher.LaunchFileAsync(file);
}

Upvotes: 1

Related Questions