Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-UWP-System.UnauthorizedAccessException:&x27;访问被拒绝;_C#_Printing_Uwp - Fatal编程技术网

C#-UWP-System.UnauthorizedAccessException:&x27;访问被拒绝;

C#-UWP-System.UnauthorizedAccessException:&x27;访问被拒绝;,c#,printing,uwp,C#,Printing,Uwp,我正在编写一个应用程序,它需要在不打开打印对话框窗口的情况下打印到USB收据打印机(因此POSPrinter不适用)。我发现了一个使用附带的exe(由@Stefan Wick to提供)从UWP应用程序中打印的示例。该程序的示例运行在我的开发笔记本电脑上。当我将相同的代码放入我的UWP应用程序时,我得到以下异常: --System.UnauthorizedAccessException:'访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))' 所有例外情

我正在编写一个应用程序,它需要在不打开打印对话框窗口的情况下打印到USB收据打印机(因此POSPrinter不适用)。我发现了一个使用附带的exe(由@Stefan Wick to提供)从UWP应用程序中打印的示例。该程序的示例运行在我的开发笔记本电脑上。当我将相同的代码放入我的UWP应用程序时,我得到以下异常:

--System.UnauthorizedAccessException:'访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))'

所有例外情况详情如下:

System.UnauthorizedAccessException HResult=0x80070005 消息=访问被拒绝。(HRESULT异常:0x80070005(E_访问被拒绝)) Source=System.Private.CoreLib 堆栈跟踪: 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在System.Runtime.CompilerServices.TaskAwaiter.GetResult()中 在POSClient.Views.SellPage.d_u60.MoveNext()中

此代码块中出现异常:

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

    }
LaunchFullTrustProcessForCurrentAppAsync
行尝试运行时

它从未到达exe,但以下是相关代码,以防它有所帮助:

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();
    }
我相信这个错误是在试图访问该文件,但它可能是在试图访问exe?我已经在文件文件夹上设置了权限,允许每个人都有读/写权限(出于测试目的),但仍然会得到相同的异常

此示例代码将所选图像打印到默认打印机。一旦我开始工作,我将进行必要的修改以打印收据

我对UWP还比较陌生,对此我感到困惑(我花了一周的时间寻找相关的东西,但仍然找不到答案)。任何意见都将不胜感激


谢谢。

在发布这篇文章后,一个无关的搜索几乎立刻给了我答案。问题出在访问exe时。我不知道如何修改包.manifest文件以提供exefullTrustProcess功能。一旦我解决了这个问题,代码就可以完美地运行了