Eric
Eric

Reputation: 139

Xamarin.Forms app need to write to a shared local storage location

I am in the process of writing a Xamarin.Forms line-of-business application. The app will be targeting UWP and Android.

I have a requirement of being able to store information and pictures taken, in a shared folder on the local storage. This way, multiple users of the same device at different times can resume work-in-progress of the first user.

I am not sure what my options are, as I am unable to write outside of AppData folder (for UWP).

I read about potentially using a Picker and storing the selected folder in the FutureAccessList for UWP, but I am unsure if it will actually work and seems hacky as I will need to come up with a way of doing the same for Android at a later time.

Any ideas/pointers are greatly appreciated!

Upvotes: 4

Views: 855

Answers (1)

Martin Zikmund
Martin Zikmund

Reputation: 39072

There is a special ApplicationData.SharedLocalFolder folder that allows you to share app data across user accounts on a PC. Its main limitation is that it requires appropriate Group Policy:

SharedLocalFolder is only available if the device has the appropriate group policy. If the group policy is not enabled, the device administrator must enable it. From Local Group Policy Editor, navigate to Computer Configuration\Administrative Templates\Windows Components\App Package Deployment, then change the setting "Allow a Windows app to share application data between users" to "Enabled."

I feel that the fact that this is not allowed by default is a great obstacle to the usefulness of this API.

There a publisher cache folder, but this solution is not appropriate for you because of documentation says:

Publisher Cache shares data across apps for the current user

So I would probably really go with the picker-based solution you proposed. Offer the user to select a folder to save the data to using the FolderPicker and then store the selected folder to the FutureAccessList. The future access list is reliable and can even track the changes of the selected item (like when the user moves it to a different location). The abstraction of the selection process in a cross-platform manner may be a bit more complicated, but it should be possible to hide it behind a dependency service implementation. My guess will provide an async method that will initialize the target location. On UWP this will check the FutureAccessList if a location was selected previously and if it was not, it will use the FolderPicker to let the user select it and will store it for future user afterward. On Android, it will work in Android specific manner (I am not sure what are the options there). Then the service will have some file manipulation methods that will abstract the platform-specific manipulation with the folder (I think you cannot use the common System.IO namespace, as you cannot directly access the user selected folder outside of the StorageFolder API)

Upvotes: 2

Related Questions