C#UWP - FileStream访问被拒绝

时间:2018-02-06 14:37:45

标签: uwp filestream imgur

我尝试读取图像,但我有以下错误:

  

“拒绝访问”C:\ User \ 53324 \ Pictures \ oldboy2.jpg“”

代码:

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

2 个答案:

答案 0 :(得分:1)

  

"拒绝访问" C:\ User \ 53324 \ Pictures \ oldboy2.jpg""

Windows应用商店应用运行沙盒,并且对文件系统的访问权限非常有限。在大多数情况下,他们只能直接访问他们的安装文件夹和他们的应用程序数据文件夹。只有通过代理流程才能访问其他位置。您可以通过FileOpenPicker访问@"C:\Users\53324\Pictures\oldboy2.jpg"

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
{

}

有关详情,请参阅Open files and folders with a picker

答案 1 :(得分:0)

试试这个:

var fileStream = new FileStream(yourFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true);
var bitmap = new BitmapImage();
await bitmap.SetSourceAsync(fileStream.AsRandomAccessStream());

应该工作。