5. Local or
Roaming
Settings File
App Creates/Manages
files and settings
Application
Files
App Data
Folder
Package
Manager
Installation Folder
WinRT Storage
APIs
Install
DB
Database file
DB Files (r/o)
6. Roaming
Folder Settings
• Other devices can
access what you put in
here
• Data roamed cross-
device
• Limited to 100kb per
application
• Held in OneDrive
storage
Local
Folder Settings
• Store local data here
for use by your
application
• Can store data up to
the limit of the storage
on the device
• Retained if the
application is updated
Temp
Folder
• Use for temporary
storage
• No guarantee it
will still be here
next time your
program runs
• Cleaned up in a
low storage
condition
PasswordVault
Credentials
• Credential Locker
• Use for secure
storage of
PasswordCredential
objects
• Data roamed cross-
device
7. 7
Windows.Storage.StorageFolder roam = Windows.Storage.ApplicationData.Current.RoamingFolder;
Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFolder temp = Windows.Storage.ApplicationData.Current.TemporaryFolder;
Windows.Storage.ApplicationDataContainer localSettings =
Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.ApplicationDataContainer roamingSettings =
Windows.Storage.ApplicationData.Current.RoamingSettings;
The StorageFolder object represents a folder and is used to access the
folder and its contents
The ApplicationDataContainer object represents a key-value pair
settings dictionary
9. 9
// Create a simple setting
localSettings.Values["exampleSetting"] = "Hello Windows";
// Read data from a simple setting
Object value = localSettings.Values["exampleSetting"];
if (value == null)
{
// No data
}
else
{
// Access data in value
}
// Delete a simple setting
localSettings.Values.Remove("exampleSetting");
11. 11
WP 8.1 App – PFN 12345
Roaming Local Temp
Windows App – PFN 12345
RoamingLocalTemp
PFN 12345
Roaming
folder
App writes data using standard
file/settings APIs.
Sync engine transfers data
periodically based on
triggers (user idle, battery,
network, etc.)
OneDrive stores up to 100kb of roaming
data per app (not included in user quota).
If app exceeds the limit, sync stops.
Other clients are notified of
updated data via Windows
Notification Service. If app is
running when sync occurs, an
event is raised.Roaming
settings
19. File Type/ API
Installation
Folder
App data folder Example
File access using
Windows.Storage
API via URIs
ms-appx:///
ms-appdata:///local/
ms-appdata:///roaming/
ms-appdata:///temp/
var file = await
Windows.StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appdata:///local/AppConfigSettings.xml"));
File access using
Windows.Storage
API via
StorageFolder
references
Windows.
ApplicationModel.
Package.Current.
InstalledLocation
Windows.Storage.
ApplicationData.
Current
.LocalFolder /
.RoamingFolder /
.TempFolder
var localFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile storageFile =
await localFolder.GetFileAsync("CaptainsLog.store");
29. 29
using (Stream stream = await notesFolder.OpenStreamForWriteAsync(filename,
CreationCollisionOption.OpenIfExists))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(Customer[]));
serializer.WriteObject(stream, customers);
}
This code serializes a collection of customers
30. 30
using (Stream stream = await notesFolder.OpenStreamForWriteAsync(filename,
CreationCollisionOption.OpenIfExists))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(Customers));
serializer.WriteObject(stream, customers);
}
This creates the serializer
The constructor is given the type of the object to store
31. 31
using (Stream stream = await notesFolder.OpenStreamForWriteAsync(filename,
CreationCollisionOption.OpenIfExists))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(Customers));
serializer.WriteObject(stream, customers);
}
This writes the collection of customers to the output stream
32. 32
using (Stream stream = await notesFolder.OpenStreamForReadAsync(filename))
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(Customers));
Customers result = serializer.ReadObject(stream) as Customers;
}
The read process is the reverse of the writing one
Note that we have to cast the result of ReadObject(Stream)
to the required type
33. 33
XmlSerializer serializer = new XmlSerializer(typeof(Customers));
serializer.Serialize(stream, customers);
XmlSerializer serializer = new XmlSerializer(typeof(Customers));
Customers result = serializer.Deserialize(stream) as Customers;
35. 35
var s = await destFile.OpenAsync(FileAccessMode.ReadWrite);
Compressor compressor =
new Compressor(s.GetOutputStreamAt(0), CompressAlgorithm.Mszip, 0);
An application can use compression when it saves and loads
data
The compression stream can be used as any other stream,
but it compresses the data as it is transferred
36. 36
var s = await destFile.OpenAsync(FileAccessMode.ReadWrite);
Compressor compressor =
new Compressor(s.GetOutputStreamAt(0), CompressAlgorithm.Mszip, 0);
There are a number of different algorithms available:
Lzms
Mszip
Xpress
XpressHuff
You can also set the block size for the compression, the
value 0 means use the default
37. 37
var s = await destFile.OpenAsync(FileAccessMode.ReadWrite);
Compressor compressor =
new Compressor(s.GetOutputStreamAt(0), CompressAlgorithm.Mszip, 0);
The Decompressor complements the Compressor and
provides an input stream that can be used to decompress
files
52. Note that this is not required for applications accessing files in their own
local/roaming/temporary storage
This is the same mechanism used for app-to-app communications through file associations
(See Session 10)
52
54. 54
var devices = Windows.Storage.KnownFolders.RemovableDevices;
var sdCards = await devices.GetFoldersAsync();
if (sdCards.Count == 0) return;
StorageFolder firstCard = sdCards[0];
These statements get a reference to the SD card on the
phone
They are part of a method that creates a file on the SD card
55. 55
var devices = Windows.Storage.KnownFolders.RemovableDevices;
var sdCards = await devices.GetFoldersAsync();
if (sdCards.Count == 0) return;
StorageFolder firstCard = sdCards[0];
We get a list of SD cards using the KnownFolders API
There will only be 0 or 1
56. 56
var devices = Windows.Storage.KnownFolders.RemovableDevices;
var sdCards = await devices.GetFoldersAsync();
if (sdCards.Count == 0) return;
StorageFolder firstCard = sdCards[0];
If there is not one present the value of the Count property
will be 0
This method returns if there are no SD Cards on the device
Your application must handle this eventuality gracefully
Not all devices have an SD card slot
The slot might not have a card fitted into it
57. 57
var devices = Windows.Storage.KnownFolders.RemovableDevices;
var sdCards = await devices.GetFoldersAsync();
if (sdCards.Count == 0) return;
StorageFolder firstCard = sdCards[0];
The card is exposed as a StorageFolder, so we can use it in
the same way the previous devices we have seen
We can create folders and files
But we can only work with files types for which we have
declared a file association in the manifest