0

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"?

StealthRT
  • 10,108
  • 40
  • 183
  • 342

1 Answers1

0

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.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • But I’m not able to do it without the user interacting with it then? – StealthRT Mar 05 '18 at 12:37
  • You could only access directories which are declared in the manifest file (e.g. Documents, Pictures, Videos folder), installation folder and Local Storage folder directly. – Nico Zhu Mar 06 '18 at 01:51
  • 1
    The "RS4" release of Windows 10 (coming sometime in 2018) will support a `broadFileSystemAccess` capability that will let your app access any file the user has access to. You can also use your old Win32 / CRT / .NET code if you want to use those APIs instead of `StorageFolder` – Peter Torr - MSFT Mar 11 '18 at 00:31