For a long time now, I have wanted to access the file system when I create a Xamarin Forms UAP/UWP application but that was actually impossible … till now. After the Windows 10 build 17134 update its possible to access the broad file system, the approach is not straight forward.
To gain access to the file system in your Xamarin Forms UAP/UWP application follow these steps
1) Go the properties of your UAP/UWP application and check the targeting, the minimum should be at least 16299, what I recommend is 171344
You can also change the targets unloading the project and editing the csproj file
2) In your solution explorer edit your Package.appxmanifest by selecting it and press F7, looking the file from the top should look like the image below
Add this namespace xmlns:rescap=”http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities and update the IgnorableNamesSpaces like this IgnorableNamespaces=”uap mp rescap” after the changes your file should look like the image below
3) Lookup for the capabilities node in the manifest and add a new capability <rescap:Capability Name=”broadFileSystemAccess” /> your capabilities section should look like the image below
4) Rebuild your application, then select it on the solution explorer, right click over it and click on deploy, this will register the application in your OS
5) on your Windows OS go to settings>File system privacy settings and you will see all the UAP/UWP applications that are registered in your OS and have access to the file system, here you can allow/deny the access to the file system in general or by application
6) now everything is ready for your app to access the file system, but there is a little catch, in most cases, you cannot use the classes in system.io to access the file system you have to use Windows.Storage.Storagefolder below is a code snippet that illustrates how to use such class
public async void GetDirectories(string sDir) { var folder = await StorageFolder.GetFolderFromPathAsync(sDir); foreach (var file in await folder.GetFilesAsync()) { Debug.WriteLine(file.Name); } }
I have created a sample app using these steps, you can download the source from GitHub
Hi Joche,
I have a Xamarin Forms app that accesses a SQLite Database (using Nuget sqlite-net-pcl). While the Android and iOS apps copy the database locally from the cloud to work on and then pass back for syncing with a centralised db, I want the UWP app to directly access the database on a network drive (an azure file storage assigned on a Win10 Desktop as W: drive). This works OK when I run debug in VS2019 but when I deploy as a package to another desktop, I get a connection error to the W: drive. I can access the db as a Windows.Storage file but it seem that SQLite needs System.IO access. Any ideas would be most welcome. Seems to me that if VSDebug can access it, a deployed package should as well?
Regards,
Kevin.