简体   繁体   中英

Unauthorized Access Exception UWP

I made a simple UWP app to test some code

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. I'm new to UWP and I don't know how to make it work...

PS. Some of the code I used come from Microsoft Samples on github...

To access the PicturesLibrary folder you need to declare it as a capability in your manifest file, like so:

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

More information on App Capability Declarations is available in the Microsoft Docs

In UWP application we can't access folder directly. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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