简体   繁体   English

未经授权的访问异常UWP

[英]Unauthorized Access Exception UWP

I made a simple UWP app to test some code 我做了一个简单的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);
            }
        }
    }
}

And it throws UnauthorizedAccessException at the StorageFolder line. 并在StorageFolder行上抛出UnauthorizedAccessException I'm new to UWP and I don't know how to make it work... 我是UWP的新手,我不知道如何使它工作。

PS. PS。 Some of the code I used come from Microsoft Samples on github... 我使用的一些代码来自github上的Microsoft Samples ...

To access the PicturesLibrary folder you need to declare it as a capability in your manifest file, like so: 要访问PicturesLibrary文件夹,您需要在清单文件中将其声明为功能,如下所示:

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

More information on App Capability Declarations is available in the Microsoft Docs Microsoft Docs中提供了有关应用程序功能声明的更多信息。

In UWP application we can't access folder directly. 在UWP应用程序中,我们无法直接访问文件夹。 They provide the capability for some folders those we can access directly. 它们提供了一些我们可以直接访问的文件夹的功能。 But if you want to access whole file system then you can add restricted capability boardFileSystem. 但是,如果要访问整个文件系统,则可以添加功能受限的boardFileSystem。 Then you can access any folder and file by its path. 然后,您可以通过其路径访问任何文件夹和文件。

For more information visit to this link 有关更多信息,请访问此链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM