C# - UWP - System.UnauthorizedAccessException: 'Access is denied.'

0

I am writing an app that needs to print to a USB receipt printer (so POSPrinter is not applicable) without raising the Print Dialog window. I have found an example that prints from within a UWP app using an included exe (provided by @Stefan Wick to this question). The sample of that program runs on my development laptop. When I put the same code in my UWP app, I get the following exception:

-- System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'

Full exception details are:

System.UnauthorizedAccessException HResult=0x80070005 Message=Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) Source=System.Private.CoreLib StackTrace: at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at POSClient.Views.SellPage.d__60.MoveNext() in

The exception occurs in this block of code:

private async void Button_Click(object sender, RoutedEventArgs e)
    {
        if (imageFile != null)
            ApplicationData.Current.LocalSettings.Values["FileToPrint"] = imageFile.Path;

        if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0))
        {
            await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
        }

    }

when the LaunchFullTrustProcessForCurrentAppAsync line attempts to run.

It never reaches the exe, but here is the pertinent code from that in case it helps:

static void Main(string[] args)
    {
        AutoResetEvent resetEvent = new AutoResetEvent(false);

        if (ApplicationData.Current.LocalSettings.Values.ContainsKey("FileToPrint"))
        {
            Image img = Image.FromFile(ApplicationData.Current.LocalSettings.Values["FileToPrint"] as string);
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += new PrintPageEventHandler((sender, e) =>
            {
                img = ResizeImage(img, e.Graphics.VisibleClipBounds.Size);
                e.Graphics.DrawImage(img, Point.Empty);
                e.HasMorePages = false;
            });
            doc.EndPrint += new PrintEventHandler((sender, e) =>
            {
                resetEvent.Set();
            });
            doc.Print();
        }
        resetEvent.WaitOne();
    }

I believe this error is in trying to access the file, but it may be in trying to access the exe? I have set permissions on the files folder to allow everyone read/write permissions (for testing purposes) but still get the same exception.

This sample code prints a selected image to the default printer. Once I get that working, I'll make the modifications necessary to print a receipt.

I'm relatively new to UWP and am stumped by this (I've spent the last week searching for anything related and still can't find an answer). Any input would be greatly appreciated!

Thank you.

c#
printing
uwp
asked on Stack Overflow Dec 26, 2018 by whomer

1 Answer

0

Almost immediately after posting this, an unrelated search gave me the answer. The problem was in accessing the exe. I had not known to modify the package.manifest file to give the exe fullTrustProcess capabilities. Once I sorted that out, the code runs perfectly.

answered on Stack Overflow Dec 26, 2018 by whomer

User contributions licensed under CC BY-SA 3.0