Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 388
WhiteIntel Avatar asked Oct 12 '15 13:10

WhiteIntel


People also ask

How do I access WindowsApps folder?

Follow these steps to view the WindowsApps folder first: Open the Windows File Explorer and go to the C:\Program Files directory. Now, click the View tab, located on top of the File Explorer. Finally, select the Hidden items checkbox to enable it.

How do I access UWP files?

Open files and folders with a picker - UWP applications Access files and folders by letting the user interact with a picker. You can use the FileOpenPicker and FileSavePicker classes to gain access to files, and the FolderPicker to gain access to a folder.

Where are Windows 10 app files located?

To find out where an app is installed, the easiest method is to go through a shortcut used to open it. to do so, right-click on a shortcut and select “Open file location”. If the shortcut is in File Explorer, or on the desktop, this will take you straight to the location of the app.


3 Answers

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

  • Directories which are declared in the manifest file (e.g. Documents, Pictures, Videos folder)
  • Directories and files which the user manually selected with the FileOpenPicker or FolderPicker
  • Files from the FutureAccessList or MostRecentlyUsedList
  • Files which are opened with a file extension association or via sharing

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...

UPDATE:

Windows 10 build 17134 (2018 April Update, version 1803) added additional file system access capabilities for UWP apps:

  • Any UWP app (either a regular windowed app or a console app) that declares an AppExecutionAlias is now granted implicit access to the files and folders in the current working directory and downward, when it’s activated from a command line. The current working directory is from whatever file-system location the user chooses to execute your AppExecutionAlias.

  • The new broadFileSystemAccess capability grants apps the same access to the file system as the user who is currently running the app without file-picker style prompts. This access can be set in the manifest in the following manner:

    xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
    ...
    IgnorableNamespaces="uap mp uap5 rescap">
    ...
    <Capabilities>
      <rescap:Capability Name="broadFileSystemAccess" />
    </Capabilities>

These changes and their intention are discussed at length in the MSDN Magazine article titled Universal Windows Platform - Closing UWP-Win32 Gaps. The articles notes the following:

If you declare any restricted capability, this triggers additional scrutiny at the time you submit your package to the Store for publication. ... You don’t need an AppExecutionAlias if you have this capability. Because this is such a powerful feature, Microsoft will grant the capability only if the app developer provides compelling reasons for the request, a description of how this will be used, and an explanation of how this benefits the user.

further:

If you declare the broadFileSystemAccess capability, you don’t need to declare any of the more narrowly scoped file-system capabilities (Documents, Pictures or Videos); indeed, an app must not declare both broadFileSystemAccess and any of the other three file-system capabilities.

finally:

Even after the app has been granted the capability, there’s also a runtime check, because this constitutes a privacy concern for the user. Just like other privacy issues, the app will trigger a user-consent prompt on first use. If the user chooses to deny permission, the app must be resilient to this.

like image 73
Rico Suter Avatar answered Sep 29 '22 20:09

Rico Suter


The accepted answer is no longer complete. It is now possible to declare broadFileSystemAccess in the app manifest to arbitrarily read the file system.

The File Access Permissions page has details.

Note that the user can still revoke this permission via the settings app.

like image 25
Sam Avatar answered Sep 27 '22 20:09

Sam


You can do it from UI in VS 2017.

Click on manifest file -> Capabilities -> Check photo library or whatever stuff you want.

enter image description here

like image 28
Fangming Avatar answered Sep 28 '22 20:09

Fangming