Original: Quickly build Windows 8 style Apps 26-local app data
This post focuses on how to get settings and file containers for your app, how to write data to settings, how to get data from settings, how to delete data in settings, how to write data to a file, and how to get data from a file.
When the app is installed, the system provides its own per-user data store for app data such as settings and files. We don't need to know where these data exist or how to store it because the system is responsible for managing the physical storage work. We just need to use the App data API.
Local app data is typically used for persistence of current device data, and local data is not limited in size, typically using local data to store large datasets.
How to get the settings and file containers for your app
1. Use the applicationdata.localsettings property to get the settings in the Applicationdatacontainer object.
Windows.Storage.ApplicationDataContainer localsettings = Windows.Storage.ApplicationData.Current.LocalSettings;
2. Use the Applicationdata.localfolder property to get the files in the Storagefolder object.
Windows.Storage.ApplicationDataContainer localfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
How to write data to Settings
We can write data to settings in three ways.
1. Use the applicationdatacontainer.values property.
localsettings.values["examplesetting" "Hello Windows";
How key-value pairs are used.
2. Use the applicationdatacompositevalue object to make a composite setting.
New Windows.Storage.ApplicationDataCompositeValue ();
composite["Intval"] = 1;
composite["Strval" "string";
localsettings.values["examplecompositesetting"] = composite;
3. Use the Applicationdatacontainer.createcontainer method to create the settings container and add the data to the container.
Localsettings.createcontainer ("Examplecontainer", Windows.Storage.ApplicationDataCreateDisposition.Always);
if (LocalSettings.Containers.ContainsKey ("Examplecontainer"))
{
localsettings.containers["Examplecontainer"]. values["examplesetting" "Hello Windows";
}
Where the enumeration value of Windows.Storage.ApplicationDataCreateDisposition always indicates that the container does not exist, it is created.
how to get data from settings
1. Use the applicationdatacontainer.values property to get the data.
value = localsettings.values["examplesetting"];
2. Use the applicationdatacontainer.values property to get the data in the composite settings.
(Windows.Storage.ApplicationDataCompositeValue) localsettings.values["examplecompositesetting"];
if null)
{
}
Else
{
}
3. Use the applicationdatacontainer.values property to get the data in the container
bool Hascontainer = LocalSettings.Containers.ContainsKey ("Examplecontainer");
BOOL false;
if (Hascontainer)
{
hassetting = localsettings.containers["Examplecontainer"]. Values.containskey ("examplesetting");
}
How to delete data in Settings
1. Use the applicationdatacontainersettings.remove method to delete data, composite data settings, and container settings.
LocalSettings.Values.Remove ("examplesetting");
How to write data to a file
Usually we use Windows.Storage.StorageFolder.CreateFileAsync and Windows.Storage.FileIO.WriteTextAsync . Create or update files in the local data store.
void Writetimestamp ()
{
New Windows.Globalization.DatetimeFormatting.DateTimeFormatter ("longtime");
StorageFile Samplefile = await Localfolder.createfileasync ("DataFile.txt"
createcollisionoption.replaceexisting);
Await Fileio.writetextasync (Samplefile, formatter. Format (DateTime.Now));
}
The replaceexisting value in creationcollisionoption indicates that the file is created if it does not exist, and is replaced if it exists.
how to get data from a file
Usually we will use Windows.Storage.StorageFolder.GetFileAsync, Windows.Storage.StorageFile.GetFileFromApplicationUriAsync and Windows.Storage.FileIO.ReadTextAsync Open or read a file in the local data store.
void Readtimestamp ()
{
Try
{
StorageFile Samplefile = await Localfolder.getfileasync ("DataFile.txt");
String timestamp = await fileio.readtextasync (samplefile);
}
Catch (Exception)
{
}
}
The relevant sample code is provided in MSDN: Application Data Sample.
Quickly build Windows 8 style Apps 26-local app data