Skip to main content
May 24, 2016
PC

Sharing your local app data



In the first installment of this blog series we looked at the various options at your disposal to store your app’s local data. In this post we will be looking at how that local data can be shared, both between apps and users.

When an app is installed, the system creates a per-user app data container on the device. This app data container is within the app sandbox, which means no other app or user will be able to access it. Fortunately, the Universal Windows Platform provides mechanisms to get around the sandbox if the scenario requires it. These mechanisms are the subject of this post.  We’ll cover how to share data across apps of the same publisher and data shared across multiple users of an app.

Sharing local data across apps

A new Windows 10 feature to share data is Publisher Cache, which allows two or more apps from the same publisher to share user data. Note that in contrast to Shared Local this feature keeps data inside the user boundary; Publisher Cache shares data across apps for the current user.

1_localapp

Using Publisher Cache

The first thing you need to do to enable Publisher Cache is to have the following declaration to every app that wants to participate in the data sharing:

[code language=”csharp”]

<Extensions>
<Extension Category="windows.publisherCacheFolders">
<PublisherCacheFolders>
<Folder Name="Downloads" />
</PublisherCacheFolders>
</Extension>
</Extensions>

[/code]

You need to call out any folder or folders you will be accessing; in this case we just have one called Downloads. Remember, you need to do this from every app that wants access to the shared folders.

To read or write files from a Publisher Cache folder, you must use the GetPublisherCacheFolder and pass the folder name as a parameter; you will get back a StorageFolder. You can then use StorageFolder.CreateFileAsync and StorageFolder.GetFileAsync to write and read files.

[code language=”csharp”]

//Create dataFile.txt in Publisher Cache folder Downloads and write “My text” to it
StorageFolder sharedDownloadsFolder = ApplicationData.Current.GetPublisherCacheFolder("Downloads");
StorageFile sampleFile = await sharedLocalFolder.CreateFileAsync("dataFile.txt");
await FileIO.WriteTextAsync(sampleFile, "My text");

//Read the first line of dataFile.txt in Publisher Cache folder Downloads
StorageFile sampleFile = await sharedDownloadsFolder.GetFileAsync("dataFile.txt");
String fileContent = await FileIO.ReadTextAsync(sampleFile);

[/code]

Again, note that data store in the Publisher Cache will only be persisted as long as an app that uses the cache folders is installed and won’t be backed up by the system. Moreover, any app that has access to a Publisher Cache folder is free to clear the folder contents.

Sharing local data across users

We introduced a new storage location Windows 10, ApplicationData.SharedLocalFolder, that allows multiple users of one app to share local data. Obviously this feature is only interesting with devices that will be used by more than one person. For such scenarios, for example in educational uses, it may make sense to place any large downloads in Shared Local. The benefits will be two-fold: any user can access these files without the need to re-download them, also there will be storage space savings.

2_localapp

Using Shared Local

To read or write files in SharedLocal, you must use the ApplicationData.SharedLocalFolder property; you will get back a StorageFolder. You can then use StorageFolder.CreateFileAsync and StorageFolder.GetFileAsync to write and read files.

[code language=”csharp”]

//Create dataFile.txt in SharedLocal and write “My text” to it
StorageFolder sharedLocalFolder = ApplicationData.Current.SharedLocalFolder;
StorageFile sampleFile = await sharedLocalFolder.CreateFileAsync("dataFile.txt");
await FileIO.WriteTextAsync(sampleFile, "My text");

//Read the first line of dataFile.txt in SharedLocalFolder
StorageFile sampleFile = await sharedLocalFolder.GetFileAsync("dataFile.txt");
String fileContent = await FileIO.ReadTextAsync(sampleFile);

[/code]

Keep in mind that Shared Local is only available if the machine has the right group policy, otherwise when you call ApplicationData.Current.SharedLocalFolder you will get back a null result.

In order to enable Shared Local the machine administrator should enable the corresponding policy.

3_localapp

Alternatively, the administrator could create a REG_DWORD value called AllowSharedLocalAppData with a value of 1 under HKLMSOFTWAREPoliciesMicrosoftWindowsCurrentVersionAppModelStateManager.

4_localapp

Note that data store in ShareLocal will only be persisted as long as the app is installed on the device and won’t be backed up by the system.

Wrapping Up

In this post, we have provided you with ways to share your local app data, both with other apps from the same publisher and with other local users of the app. Hopefully, you’ll be able to use this information to create experiences that delight your users.

Additional Resources