【问题标题】:Unauthorized Access Exception UWP未经授权的访问异常 UWP
【发布时间】:2018-08-21 16:28:21
【问题描述】:

我做了一个简单的 UWP 应用来测试一些代码

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        FlipButton.Click += new RoutedEventHandler(FlipButton_Click);
    }

    private async void FlipButton_Click(object sender, RoutedEventArgs e)
    {
        var sf = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///test_pattern.png"));
        var original = await sf.OpenStreamForReadAsync();

        using (var stream = new SKManagedStream(original))
        using (var bitmap = SKBitmap.Decode(stream))
        {
            ITransform flip = new Flip(FlipOrientation.Vertical);
            SKBitmap result = flip.Perform(bitmap);

            StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.PicturesLibrary);
            StorageFile flipfile = await storageFolder.CreateFileAsync("flip_vertical.png", CreationCollisionOption.ReplaceExisting);
            Stream flipstream = await flipfile.OpenStreamForWriteAsync();

            using (SKManagedWStream wstream = new SKManagedWStream(flipstream))
            {
                result.Encode(wstream, SKEncodedImageFormat.Png, 100);
            }
        }
    }
}

它在StorageFolder 行抛出UnauthorizedAccessException。我是 UWP 的新手,我不知道如何使它工作......

PS。我使用的一些代码来自 github 上的 Microsoft Samples...

【问题讨论】:

  • 不要使用...ForUser API,并以用户身份传递null。相反,只需使用非...ForUser API(或实际传入真实用户的对象)。通过null 不太可能有好的结局。

标签: c# exception io uwp storage


【解决方案1】:

在 UWP 应用程序中,我们不能直接访问文件夹。它们为我们可以直接访问的某些文件夹提供了功能。但是如果你想访问整个文件系统,那么你可以添加受限功能 boardFileSystem。然后您可以通过路径访问任何文件夹和文件。

欲了解更多信息,请访问此链接

https://docs.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations

【讨论】:

    【解决方案2】:

    要访问 PicturesLibrary 文件夹,您需要在清单文件中将其声明为一项功能,如下所示:

    <Capabilities><uap:Capability Name="picturesLibrary"/></Capabilities>
    

    有关应用功能声明的更多信息,请访问Microsoft Docs

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多