C#:UnauthorizedAccessException-如何授予对读取文件的访问权限?

时间:2019-07-21 01:35:33

标签: c# permissions uri bitmapimage

首先,我绝对是C#的初学者,总体而言,我对Windows没有太多的经验。因此,如果以前在其他地方回答过此问题,对不起我找不到它。

我正在尝试制作一个UWP应用,该应用显示的图片将在您单击按钮后被替换。

所以我的xaml是:

<Image x:Name="goodboi" Source="Assets/Dogs/husky.jpg"/>

<Button x:Name="accept" Click="Accept_Click">
    <Image Source="Assets/greenH.png"/>
</Button>

我的代码是:

string images = @"D:\Users\leuchtmarker\source\repos\Shelter\Shelter\Assets\Dogs";
Random random = new Random();
private void Accept_Click(object sender, RoutedEventArgs e)
        {
            string[] imagesPath = System.IO.Directory.GetFiles(images, ".jpg");

            BitmapImage bitmapImage = new BitmapImage(new Uri(imagesPath[random.Next(imagesPath.Length)]));
            goodboi.Source = bitmapImage;
        }

点击按钮后,出现此错误:

System.UnauthorizedAccessException: 'Access to the path 'D:\Users\leuchtmarker\source\repos\Shelter\Shelter\Assets\Dogs' is denied.'

该如何解决?

0 个答案:

没有答案