Search code examples
uwpfilestreamimgur

C# UWP - FileStream access Denied


I try to read an image but I have this following error :

"Access Denied to "C:\User\53324\Pictures\oldboy2.jpg""

code :

await Task.Run(async () =>
{
    using (var fs = new FileStream(@"C:\Users\53324\Pictures\oldboy2.jpg", FileMode.Open))
    {
        image = await endpoint.UploadImageStreamAsync(fs);
    }
    Debug.Write("Image uploaded. Image Url: " + image.Link);
});

Solution

  • "Access Denied to "C:\User\53324\Pictures\oldboy2.jpg""

    Windows Store apps run sandboxed and have very limited access to the file system. For the most part, they can directly access only their install folder and their application data folder. Access to other locations is available only through a broker process. You could access @"C:\Users\53324\Pictures\oldboy2.jpg" via FileOpenPicker.

    var picker = new Windows.Storage.Pickers.FileOpenPicker();
    picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");
    
    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
    if (file != null)
    {
        // Application now has read/write access to the picked file
    
    }
    else
    {
    
    }
    

    For more, you could refer Open files and folders with a picker.