WhiteIntel WhiteIntel - 13 days ago 6
C# Question

Windows 10 Universal App File/Directory Access

I´m developing an app that is reading jpeg and pdf files from a configurable location on the filesystem.
Currently there is a running version implemented in WPF and now I´m trying to move to the new Windows Universal apps.

The following code works fine with WPF:

public IList<string> GetFilesByNumber(string path, string number)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));

if (string.IsNullOrWhiteSpace(number))
throw new ArgumentNullException(nameof(number));

if (!Directory.Exists(path))
throw new DirectoryNotFoundException(path);

var files = Directory.GetFiles(path, "*" + number + "*",
SearchOption.AllDirectories);

if (files == null || files.Length == 0)
return null;
return files;
}


With using Universal Apps I ran into some problems:


  • Directory.Exists
    is not available

  • How can I read from directories outside of my app storage?



To read from an other directory outside the app storage I tried the following:

StorageFolder folder = StorageFolder.GetFolderFromPathAsync("D:\\texts\\");
var fileTypeFilter = new string[] { ".pdf", ".jpg" };
QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderBySearchRank, fileTypeFilter);
queryOptions.UserSearchFilter = "142";
StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(queryOptions);
IReadOnlyList<StorageFile> files = queryResult.GetFilesAsync().GetResults();


The thing is: It isn´t working, but I get an exception:


An exception of type 'System.UnauthorizedAccessException' occurred in TextManager.Universal.DataAccess.dll but was not handled in user code
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))


I know that you have to configure some permissions in the manifest, but I can´t find one suitable for filesystem IO operations...

Did someone also have such problems/a possible solution?

Solution:
From the solutions that @Rico Suter gave me, I chosed the FutureAccessList in combination with the FolderPicker. It is also possible to access the entry with the Token after the program was restarted.

I can also recommend you the UX Guidlines and this Github sample.

Thank you very much!

Answer

In UWP apps, you can only access the following files and folders:

If you need access to all files in D:\, the user must manually pick the D:\ drive using the FolderPicker, then you have access to everything in this drive...

Comments