Windows 10 Universal App File/Directory Access
Asked Answered
S

6

25

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!

Stogy answered 12/10, 2015 at 13:47 Comment(2)
Why do you want to read from directories outside your app's storage? Sandboxing exists for a reason.Luana
Because I want an easy way to change the files. So this means, there is no way because of sandboxing?Stogy
T
51

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

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.

Teth answered 12/10, 2015 at 14:6 Comment(10)
Was about to post almost exactly the same answer. I would also add that you should declare in your manifest that you want access to Pictures Library and Removable Storage. In that way you have access to most of the photos...Chargeable
Would it be possible to select the folder a single time and save the returned value in a config file to reuse the path?Stogy
I think you can let the user pick the folder once, and put it into the FutureAccessList so that you can always access it afterwards...Teth
The Documents library isn't actually accessible, is it? I think it was removed in Windows 10.Tupungato
Is there any way to hack it? Simulate user picker? Assign disk as removable storage? Access it from libraries? I want to access all files on my raspberry pi from my app... This is madness that I cant do that...Rone
@user007: I hope there is no way to circumvent this as this would be a major security issue...Teth
Yes... That's why I installed Linux on my raspberry and use mono... I am Windows user, sad that I have to use Linux...Rone
Relates this also to drag& drop ? #41847311Indenture
If it's not a security issue for classic desktop apps, why would it be a security issue for universal apps? On a related note, you can technically access all files in special directories if your app declares a capability for it. For instance, if you declare capability for pictures library, you can access all files in the corresponding directory without using a file picker. I know of no other way to do this for outlying directories, though.Endosperm
@JamesM it IS a security issue for classic desktop apps, how did you get the idea that unrestricted access to all files (that are accessible by the user) is not a security risk? However, classic desktop apps don't have such kinds of mechanism because at the time when they were introduced security wasn't a big topic and they didn't add restrictions because it's basically impossible to do that without breaking most existing software.Hummel
L
5

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.

Loki answered 25/5, 2018 at 0:15 Comment(0)
S
1

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

Skillet answered 3/2, 2019 at 1:34 Comment(0)
M
0

According to MSDN doc : "The file picker allows an app to access files and folders, to attach files and folders, to open a file, and to save a file."

https://msdn.microsoft.com/en-us/library/windows/apps/hh465182.aspx

You can read a file using the filepicker through a standard user interface.

Regards

Misteach answered 12/10, 2015 at 14:8 Comment(0)
T
0

this is not true: Files which are opened with a file extension association or via sharing try it, by opening files from mail (outlook) or from the desktop... it simply does not work you first have to grant the rights by the file picker. so this ist sh...

Typhoeus answered 7/6, 2016 at 9:24 Comment(0)
F
0

This is a restricted capability. Access is configurable in Settings > Privacy > File system. and enable acces for your app. Because users can grant or deny the permission any time in Settings, you should ensure that your app is resilient to those changes. If you find that your app does not have access, you may choose to prompt the user to change the setting by providing a link to the Windows 10 file system access and privacy article. Note that the user must close the app, toggle the setting, and restart the app. If they toggle the setting while the app is running, the platform will suspend your app so that you can save the state, then forcibly terminate the app in order to apply the new setting. In the April 2018 update, the default for the permission is On. In the October 2018 update, the default is Off.

More info

Forgave answered 30/6, 2019 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.