Small is smart
or
How to saturate your data storage greediness
Windows 10 Universal Windows Platform (UWP) provides a common app platform available on every device that runs Windows 10. This means you can create a single app package that can be installed onto a wide range of devices. And, with that single app package, the Windows Store provides a unified distribution channel to reach all the device types your app can run on.
The wide range of devices is directly related to a wide range of storage sizes and different network bandwidth. This is why we have to think about delivery process and how a large amount of data can be delivered to the client.
Download
One option is to use web service and to download the needed data after install. This is the only possible option in case when you want to have a different set of files based on the device variability.
Your application install package will not contain any large files. After install is completed your application will check the type of the device, the screen size and the storage available. With all those data you can decide what size-version is best for this device.
The negative side of this is that you have to support a web service where you will publish different size packages for each supported device. This may be expensive…
ZIP
The second option is to deliver compressed data with your installation package. This option is only possible when your resource files are with higher compression rate.
Important is to remark that installation package is also compressed, so this approach will not shrink it in size. In opposite – if you have internal ZIP packages for different device types or screen sizes, this will increase the size.
The benefit is that after install is completed your application will consume less space as data will remain compressed.
At any given moment you can extract one or several files using System.IO.Compression static classes.
There is only one remaining question – where to export hem?
Local vs Roaming
As you know UWP is designed with local and roaming app data storage.
If you use roaming data in your app, your users can easily keep your app’s app data in sync across multiple devices. If a user installs your app on multiple devices, the OS keeps the app data in sync, reducing the amount of setup work that the user needs to do for your app on their second device. Roaming also enables your users to continue a task, such as composing a list, right where they left off even on a different device. The OS replicates roaming data to the cloud when it is updated, and synchronizes the data to the other devices on which the app is installed.
You can access local and roaming files using URI schemes:
ms-appx:// ms-appx-web:// ms-appdata:///local/ ms-appdata:///temp/ ms-appdata:///roaming/
The other option is access them from code using:
Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation; Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; Windows.Storage.StorageFolder temporaryFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder; Windows.Storage.StorageFolder roamingFolder = Windows.Storage.ApplicationData.Current.RoamingFolder;
Important remark is to know you can’t access security critical data in case it’s stored into roaming folder.
To explain this I will give example with WebView.
You can use it to show any HTML formatted content, like Privacy Policy, Terms and Conditions, Help, Etc.
try { string url = @"ms-appdata:///local/index.html"; Uri targetUri = new Uri(url); webView1.Navigate(targetUri); } catch (FormatException ex) { // Bad address. }
This will work… But if you try to store data into roaming and use URL like:
string url = @"ms-appdata:///roaming/index.html";
… this will not work for HTML because this is a file with open handler.
You can check the guidelines for roaming data.
So, finally you ca use ZipFile to access files only when you need them
using (ZipArchive package = ZipFile.OpenRead("small.zip")) { ZipArchiveEntry privacy = package.GetEntry("privacy.html"); privacy.ExtractToFile(temporaryFolder.Path + "\\privacy.html"); string privacyUrl = @"ms-appdata:///temp/bible/privacy.html"; webView1.Navigate(new Uri(privacyUrl)); }
Don’t forget to dispose the file at the end.