Windows 10 Universal App File/Directory Access

Windows 10 Universal App File/Directory Access

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.

Creating a new directory in a Windows 10 Universal App UWP

Every application runs under (by) a specific user and inherit it's privileges, authorities, limitations and ...
so
The User that your app runs under it, haven't enough privilege and can't create a folder
SO you can:

  • Use Impersonation (search impersonation c#) and code your app to runs as another User with required privilege (Like Administrator). after that your application always runs as administrator (or specific user) automatically. (If you impersonation your app as Administrator, Be aware of Security issues)
  • Run your application as Administrator (or a user with enough privilege) manually. (for Administrator right click on your-app.exe and click on "Run as ...")
  • Change security settings and access limits for your working directories (e.g C:\Users[username]\Documents\MyApp\bin\x86\Debug\AppX\newFolder ) and give write access to your username

is it possible to access files in local directories? (Universal Windows Platform)

https://channel9.msdn.com/Events/Build/2017/B8012

As of creator's update, at minute 32:45 or so in that video, they confirm that the answer is "none" -- and one of the things they note you cannot do is "direct access to file system." That was some months ago, and maybe you're working with a more recent version. But as a starting point, I think it's safe to say that file access, if it's there at all, is a new feature. I haven't sat down to see what's possible in the most recent releases, but if you're not at the very cutting edge, safe to say you cannot access the file system?

Access files and folders of Internal Computer drives in CrossPlatform UWP

Figured it out, I had to remove the other storage related capabilities.

<uap:Capability Name="musicLibrary"/>
<uap:Capability Name="removableStorage"/>
<uap:Capability Name="picturesLibrary"/>
<uap:Capability Name="videosLibrary"/>
<uap:Capability Name="documentsLibrary"/>

After that I built, stopped it, unchecked and rechecked the file permissions in system settings than built and it worked.

UWP apps accessing files from random location on system

Considering this method..

    public async static Task<byte[]> ToByteArray(this StorageFile file)
{
byte[] fileBytes = null;
using (IRandomAccessStreamWithContentType stream = await file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];

using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}

return fileBytes;
}

This class..

    public class AppFile
{
public string FileName { get; set; }
public byte[] ByteArray { get; set; }
}

And this variable

    List<AppFile> _appFiles = new List<AppFile>();

Just..

    var fileOpenPicker = new FileOpenPicker();
IReadOnlyList<StorageFile> files = await fileOpenPicker.PickMultipleFilesAsync();

foreach (var file in files)
{
var byteArray = await file.ToByteArray();
_appFiles.Add(new AppFile { FileName = file.DisplayName, ByteArray = byteArray });
}

UPDATE

using Newtonsoft.Json;
using System.Linq;
using Windows.Security.Credentials;
using Windows.Storage;

namespace Your.Namespace
{
public class StateService
{
public void SaveState<T>(string key, T value)
{
var localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values[key] = JsonConvert.SerializeObject(value);
}

public T LoadState<T>(string key)
{
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey(key))
return JsonConvert.DeserializeObject<T>(((string) localSettings.Values[key]));
return default(T);
}

public void RemoveState(string key)
{
var localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.ContainsKey(key))
localSettings.Values.Remove((key));
}

public void Clear()
{
ApplicationData.Current.LocalSettings.Values.Clear();
}
}
}

How to access *.lnk files, (shortcuts) from a UWP app

but no examples using Kernal32 methods

For this scenario, you could use desktop-bridge desktop extension to approach, for detail steps please refer stfan's blog UWP with Desktop Extension.

For using Kernal32 methods, please refer pinvoke document.

public enum FINDEX_INFO_LEVELS
{
FindExInfoStandard = 0,
FindExInfoBasic = 1
}
public enum FINDEX_SEARCH_OPS
{
FindExSearchNameMatch = 0,
FindExSearchLimitToDirectories = 1,
FindExSearchLimitToDevices = 2
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WIN32_FIND_DATA
{
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
public uint dwReserved0;
public uint dwReserved1;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public string cAlternateFileName;
}

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindFirstFileEx(
string lpFileName,
FINDEX_INFO_LEVELS fInfoLevelId,
out WIN32_FIND_DATA lpFindFileData,
FINDEX_SEARCH_OPS fSearchOp,
IntPtr lpSearchFilter,
int dwAdditionalFlags);

Windows 10 Mobile: how can I see inside on my app folder?

We can use App File Explorer to view and manipulate files stored by your sideloaded apps. This tool should be able to work no matter your application is deployed in Debug mode or Release mode. But please note that this is a new tool added to Device Portal in the Anniversary Update (AU).

For how to use this tool, please see Using the App File Explorer to see your app data and also Device Portal for Mobile.

If your mobile's OS Version is earlier than Anniversary Update such as OS Build 10586, then you can use IsolatedStorageExplorer (ISE) from Windows Phone 8.1 or some other open-source tools like IsoStoreSpy and Windows Phone Power Tools.

Update:

Thanks for your feedback. I can reproduce your issue when I test with Windows 10 Mobile OS Build 10.0.14915.1000.

Sample Image

Here, to access with USB, the address should be https://127.0.0.1:10443. As it is said in Device Portal for Mobile:

USB: http://127.0.0.1:10080

Use this address when the phone is connected to a PC via a USB connection. Both devices must have Windows 10, Version 1511 or later.

So when we connected via USB, we need use http://127.0.0.1:10080 and if we enabled authentication, it will automatically redirect to https://127.0.0.1:10443.

After refreshing this page, I can see LocalAppData under User files

Sample Image
However, there is nothing in LocalAppData.

Sample Image
This seems to be an Insider Preview issue as App File Explorer works well in latest RTM OS version (Build 14393). For Insider Preview issues, please feel free to share feedbacks with Microsoft. You can report this issue with Windows Feedback app. Your feedback has made Windows better!

And as a workaround, you can try with the tools I've mentioned above or using Device Portal core API like following:

https://127.0.0.1:10443/api/filesystem/apps/files?knownfolderid=LocalAppData&packagefullname=3f828c97-6b7d-4068-8f9c-710df704c8ff_1.0.0.0_arm__9wkhgz7fyfewr

App File Explorer also built on top of these APIs. You can change packagefullname to see the app you are interested in. And you can use

GET /api/app/packagemanager/packages

to get associated details of installed packages.

For more info, please see Device Portal core API reference.



Related Topics



Leave a reply



Submit