You cannot access disk drives directly from your windows metro apps. Extracted from File access permissions in windows store apps
You can access certain file system locations, like the app install
directory, app data locations, and the Downloads folder, with Windows
Store apps by default. Apps can also access additional locations
through the file picker, or by declaring capabilities.
But there are some special folders which you can access like Pictures library
, documents library etc. by enabling capabilities from your package manifest file. So, this code will work after enabling pictures library from manifest file (copy about.png file in pictures library folder)
private async void SetImageSource()
{
var file = await
Windows.Storage.KnownFolders.PicturesLibrary.GetFileAsync("about.png");
var stream = await file.OpenReadAsync();
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
image1.Source = bitmapImage;
}
But ideal solution would be to include you file in you application and set its build action to Content so that it can be copied in your Appx folder along with other content files. Then you can set the image source like this -
public MainPage()
{
this.InitializeComponent();
Uri uri = new Uri(BaseUri, "about.png");
BitmapImage imgSource = new BitmapImage(uri);
this.image1.Source = imgSource;
}
OR you can simply do this in XAML only :
<Image x:Name="image1" Source="ms-appx:/about.png"/>
Here is list of special folders which you can access from your application -
- Local App data
- Roaming app data
- Temporary app data
- App installed location
- Downloads folder
- Documents library
- Music library
- Pictures library
- Videos library
- Removable devices
- Home group
- Media Server devices
To enable capabilities from your manifest file, double click on Package.appxmanifest
file in your solution and check Pictures Library
checkbox under capabilities tab to enable it for your application. Likewise you can do it for other folders which you want to access.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…