WinRT application and looping through a directory

113 Views Asked by At

I'm working on my first WinRT app and I do not seem to be able to find any code that would allow me to loop through a directory and get file names that are in that directory?

I have found plenty of code to do it in a normal winform, wpf and console but nothing really for the Winrt variety.

The closest I've come to code:

Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");

But that just seems to get files that are withinn my own project?

How would I go about scanning a normal directory like "c:\something\something"?

1

There are 1 best solutions below

3
Nico Zhu On

I'm working on my first WinRT app and I do not seem to be able to find any code that would allow me to loop through a directory and get file names that are in that directory?

If you want to loop through a directory within UWP, you could use GetFilesAsync to get a file list from a directory.

However, UWP 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:\something\something"via FileOpenPicker or FolderPicker.

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
{

}

And this is official tutorial you could refer to.