Adding full filesystem-access into your UWP apps

Jonas Hundertmark
medialesson
Published in
2 min readNov 3, 2023

Well well well, if it isn’t another esoteric workaround for UWP.

If you’ve built an UWP app before, you know that accessing your filesystem isn’t as easy as just using C#’s Directory-/File-classes. This is, of course, by design. UWP apps are sandboxed to a degree, and if you need a particular file from your system you are supposed to use the FilePicker or FolderPicker classes respectively. This is to ensure UWP apps don’t arbitrarily edit files without you giving explicit permission. Any calls to File or Directory will silently fail on UWP (they’ll just return null every time).

There is a workaround to this, which I will cover in this blogpost. Just be aware that adding the capabilities for full filesystem access will most likely mean that you won’t be able to publish your UWP app through the Windows Store anymore. There’s a reason filesystem access is a restricted capability.

So first up, open your Package.appxmanifest with a text/code editor of your choice. The file should be located in your app’s root directory. Inside the Package XML-Tag, add the following line:

xmlns:rescap=”http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

Furthermore, add rescap to the list of your IgnorableNamespaces (also inside the Package-Tag.

Lastly, under your app’s Capabilities add the following:

<rescap:Capability Name=”broadFileSystemAccess” />

Once you’ve successfully enabled Broad Filesystem Access in your app (it should build normally), it’s time to use it. On UWP, you will need to use the StorageFolder and StorageFile-classes instead of File and Directory respectively. They probably work a little bit different to your current implementation, but you should get the hang of it fairly quickly. Do note that all file/folder operations are now async, so your methods need to be asynchronous as well. Here’s an example implementation of a class that accesses an arbitrary folder from your filesystem, the same way Directory.Exists would check for a folder:

Now here’s the tricky part: After you’ve built your app and tried to open a file usind StorageFile/StorageFolder-Classes, you will probably get an UnauthorizedException for doing so. Turns out, we need explicit permission to access the FileSystem from our user, and that permission will be turned off by default.

On your PC, navigate to Start > Settings > Privacy > File system. On Windows 11, the Privacy tab has been ranamed to Privacy & security. Your new UWP app should be listed after being started once. Finally, enable File system access for your app.

If all those convoluted workarounds didn’t tip you off already: Broad Filesystem Access is not recommended on UWP unless you absolutely need it for your apps. Like always, please use permissions responsibly, and don’t use StorageFile to load a single File from your drive when you could just as well have used a FilePicker.

That’s all for today, Cheers!

--

--