Partilhar via


Managing App Data across app users and apps from the same publisher

In the last blog we looked at how to create settings and files that can be roamed or backed up. Let's take this a step further. What if your application has data that can be shared across multiple users of your app on a Windows Device? For example in our SimpleReader application, you can envision multiple users of the app on a shared PC. Instead of each user having their own offline copy of the feeds, wouldn't it be awesome if all the users used one copy? Now taking this a step further, what if you have a suite of applications that could have common data that they could share? Let me introduce two concepts that are available in the Universal Windows Platform to support these scenarios.

SharedLocalFolder

This is another StorageFolder that is available in your application data container. This StorageFolder is shared across all the users of your app on a machine. So you simply get an object to this StorageFolder and write to it. For example,

 StorageFolder mySharedLocalFolder = await ApplicationData.Current.SharedLocalFolder.CreateFolderAsync("AppDataBlog", CreationCollisionOption.OpenIfExists);await file.CopyAsync(mySharedLocalFolder, "MyCoolArticle.txt", NameCollisionOption.ReplaceExisting);

Note that 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.". After the group policy is enabled, SharedLocalFolder can be accessed.

If the group policy is set, if you login into to the device as a different user and install the same app and try to enumerate the contents of SharedLocalFolder, you will get the file that you just created. Here is how you get a handle to the files in the SharedLocalFolder of your app.

 myStorageFolder = await ApplicationData.Current.SharedLocalFolder.GetFolderAsync("AppDataBlog");

IReadOnlyList<StorageFile> fileList = await myStorageFolder.GetFilesAsync();
foreach (StorageFile file in fileList)
{
   //enumerate files and you will get "MyCoolArticle.txt" that was created by the other user
}

PublisherCacheFolder

If you have a suite of applications that you publish, there can be interesting scenarios where app data can be shared across apps on the same device. PublisherCacheFolder is a Storage location that is available across apps from the same publisher. The Universal Office Apps put the fonts that are downloaded in the PublisherCacheFolder . This way the fonts downloaded by Word are also available for Powerpoint this saving 100's of mb of diskspace! To illustrate this concept, I have a SimpleLockScreen app on GitHub. The images that I download as in my SimpleReader app can be accessed in my SimpleLockScreenApp. As an app publisher, you can register your app to share a storage folder with other apps that you publish by adding extensions to the app manifest.

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

The shared storage folder for the app publisher is automatically provisioned when the user installs the first app from the publisher.

To access data in the PublisherCacheFolder, it is as simple as

 StorageFolder sharedPictures =  Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("LikedPictures");

Hope you are able to take these features and apply it to build great experiences in your apps so that your users get the feeling that the settings and files they customized or created are always present and up to date.

Cheers!

Sandeep George, Program Manager - Windows Developer Platform