Windows 8 Store Apps Learning (application) file operations in data and package

Source: Internet
Author: User
Tags file system xmlns

File system: File operation in application Data, file operation in Package

Introduced

Re-imagine the Windows 8 Store Apps File system

File operations in Application data (application Datastore)

Application The settings action in the data (application Datastore)

Referencing media (Pictures, videos, or audio) in application data (application Datastore) by URI

accessing Files in Package

Accessing Removable Storage

Example

1. Demonstrates how to manipulate files in application data (application Datastore)

Filesystem/appdata/filedemo.xaml.cs

/*
* Demonstrates how to manipulate files in application data (application Datastore)
*
* ApplicationData-classes that manipulate application data stores
* Current-Returns the ApplicationData object
* Localfolder-Returns the Storagefolder object. Local storage, permanently saved
* Save path:%userprofile%\appdata\local\packages\{packageid}\localstate
* Roamingfolder-Returns the Storagefolder object. Roaming storage, same Microsoft account the same application is automatically synchronized between different devices
* Save path:%userprofile%\appdata\local\packages\{packageid}\roamingstate
* Temporaryfolder-Returns the Storagefolder object. temporary storage, which the system can remove when needed
* Save path:%userprofile%\appdata\local\packages\{packageid}\tempstate
* Roamingstoragequota-The maximum size of data synchronized from roaming storage to the cloud, read-only
* Clearasync ()-delete data in application * Clearasync (applicationdatalocality locality)-delete data from the specified store
* applicationdatalocality.local, applicationdatalocality.roaming, applicationdatalocality.temporary
*
* DataChanged-Events triggered when roaming data is synchronized from the server to the app
* Signaldatachanged ()-Force trigger DataChanged Event
*/
Using System;
Using Windows.storage;
Using Windows.UI.Core;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
Using Windows.UI.Xaml.Navigation;
Namespace XamlDemo.FileSystem.AppData
{
Public sealed partial class Filedemo:page
{
Storagefolder _roamingfolder = ApplicationData.Current.RoamingFolder;
Public Filedemo ()
{
This. InitializeComponent ();
}
protected override void Onnavigatedto (NavigationEventArgs e)
{
ApplicationData.Current.DataChanged + = current_datachanged;
}
protected override void Onnavigatedfrom (NavigationEventArgs e)
{
ApplicationData.Current.DataChanged-= current_datachanged;
}
Async void Current_datachanged (applicationdata sender, Object args)
{
Await Dispatcher.runasync (Coredispatcherpriority.normal, () =>
{
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "DataChanged event is triggered";
});
}
Private async void Btnreadwrite_click_1 (object sender, RoutedEventArgs e)
{
Write
Storagefile FileWrite = await _roamingfolder.createfileasync (@ "Webabcdtest\readwritedemo.txt", creationcollisionoption.replaceexisting);
Await Fileio.writetextasync (FileWrite, "I am WEBABCD:" + DateTime.Now.ToString ());
Read
Storagefile Fileread = await _roamingfolder.getfileasync (@ "Webabcdtest\readwritedemo.txt");
ms-appdata:///local/, ms-appdata:///roaming/, ms-appdata:///temp/
Storagefile Fileread = await storagefile.getfilefromapplicationuriasync (new Uri ("ms-appdata:///roaming/webabcdtest/ ReadWriteDemo.txt ", Urikind.absolute));
String textcontent = await fileio.readtextasync (fileread);
Lblmsg.text = textcontent;
Force DataChanged event to be triggered (demo only, not required in actual project)
ApplicationData.Current.SignalDataChanged ();
}
}
}

2. Demonstrates how to operate on the settings in Application data (application Datastore)

Filesystem/appdata/settingsdemo.xaml

<page x:class= "XamlDemo.FileSystem.AppData.SettingsDemo" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" using: XamlDemo.FileSystem.AppData "xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "mc:ignorable=" D "> <grid background=" Transparent "&
        Gt
    
            <stackpanel margin= "0 0 0" > <textblock name= "lblmsg" fontsize= "14.667"/>
    
            <button name= "Btnreadwrite" content= "Read and write Settings" click= "btnreadwrite_click_1" margin= "0 0 0"/> <button name= "Btnreadwritewithcontainer" content= "group Settings" click= "Btnreadwritewithcontainer_click_1" Margin= "0 0 0"/> <button name= "Btnreadwritewithcomposite" content= "father and son Settings" click= "Btnreadwritewit
    
 Hcomposite_click_1 "margin=" 0 0 0 "/>           <button name= "BtnSetVersion0" content= sets the version number of application Data to 0 "click=" btnsetversion0_click_1 "margin=" 0 0 0 "/> <button name=" BtnSetVersion1 "content=" sets the version number of application Data to 1 "click=" btnsetvers Ion1_click_1 "margin=" 0 0 0 "/> </StackPanel> </Grid> </Page>

Filesystem/appdata/settingsdemo.xaml.cs

/*
* Demonstrates how to operate on the settings in Application data (application Datastore)
*
* ApplicationData-classes that manipulate application data stores
* Current-Returns the ApplicationData object
* LocalSettings-Returns the Applicationdatacontainer object. Local storage, permanently saved
* Save path:%userprofile%\appdata\local\packages\{packageid}\settings
* Roamingsettings-Returns the Applicationdatacontainer object. Roaming storage, same Microsoft account the same application is automatically synchronized between different devices
* Save path:%userprofile%\appdata\local\packages\{packageid}\settings
* Version-Gets the current application number, the default value is 0, read-only (version control for local settings data)
* Setversionasync ()-Specifies the version number of the current application data (for version control for local setting)
* * Applicationdatacontainer-classes that manipulate the settings data
* Name-Names of containers, defaults to Empty * CreateContainer (string name, applicationdatacreatedisposition disposition)-activates one for saving settings data Container, that is, grouping settings data
* Name-Names of containers
* Disposition-how the container is activated: Always-always active; Existing-container presence is activated
* Containers-Container Collection
* Deletecontainer ()-Deletes the specified container
* Values-Save "settings" data, a dictionary table
* Its data can be a applicationdatacompositevalue type of data, Applicationdatacompositevalue is also a dictionary table, so that multiple "settings" data can be placed in a key
* *
Notes
* When key is highpriority, the system synchronizes highpriority data (support Applicationdatacompositevalue data) among multiple devices at the fastest speed.
* Examples are as follows:
* applicationdatacontainer.values["highpriority" = "The value here will be synchronized between multiple devices at the fastest speed of the system";
*/
Using System;
Using Windows.storage;
Using Windows.UI.Core;
Using Windows.UI.Xaml;
Using Windows.UI.Xaml.Controls;
Namespace XamlDemo.FileSystem.AppData
{
Public sealed partial class Settingsdemo:page
{
Public Settingsdemo ()
{
This. InitializeComponent ();
}
Demo of Action "Settings" data
private void Btnreadwrite_click_1 (object sender, RoutedEventArgs e)
{
Applicationdatacontainer localsettings = ApplicationData.Current.LocalSettings;
Save Settings data
localsettings.values["key"] = "I am WEBABCD";
If key is highpriority, the system will synchronize highpriority values among multiple devices at the highest priority speed
localsettings.values["highpriority"] = "I am WEBABCD";
Deletes the specified settings data
LocalSettings.Values.Remove ("key");
Get the settings data
Lblmsg.text = (string) localsettings.values["key"];
}
Group settings data, which holds different data in different containers
private void Btnreadwritewithcontainer_click_1 (object sender, RoutedEventArgs e)
{
To activate a container named GroupName in LocalSettings
Applicationdatacontainer container = ApplicationData.Current.LocalSettings;
Applicationdatacontainer localsettings = container. CreateContainer ("GroupName", applicationdatacreatedisposition.always);
Delete the specified container
Container. Deletecontainer ("GroupName");
Save settings data within a container
if (container. Containers.containskey ("groupname"))
{
Container. Containers["GroupName"]. values["key"] = "I am WEBABCD";
}
Gets the settings data from the specified container
Lblmsg.text = (string) container. Containers["GroupName"]. values["Key"];
Lblmsg.text + = Environment.NewLine;
Gets the settings data from the specified container
Lblmsg.text + = (string) localsettings.values["key"];
}
Parent-Child "settings" data, that is, the data in the key is a Applicationdatacompositevalue object, and Applicationdatacompositevalue is also a dictionary table
private void Btnreadwritewithcomposite_click_1 (object sender, RoutedEventArgs e)
{
Applicationdatacontainer localsettings = ApplicationData.Current.LocalSettings;
Save the parent-child "settings" data
Applicationdatacompositevalue parent1 = new Applicationdatacompositevalue ();
parent1["child1"] = "abc";
parent1["child2"] = "xyz";
localsettings.values["parent1"] = parent1;
Parent-Child "settings" data acquisition
Lblmsg.text = (String) ((applicationdatacompositevalue) localsettings.values["Parent1"]) ["Child1"];
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = (string) ((applicationdatacompositevalue) localsettings.values["Parent1"]) ["Child2"];
}
Private async void Btnsetversion0_click_1 (object sender, RoutedEventArgs e)
{
Set the version number of application Data to 0 and execute the specified method
Await ApplicationData.Current.SetVersionAsync (0, New Applicationdatasetversionhandler (Setversionhandler));
}
Private async void Btnsetversion1_click_1 (object sender, RoutedEventArgs e)
{
Set the version number of application Data to 1 and execute the specified method
Await ApplicationData.Current.SetVersionAsync (1, New Applicationdatasetversionhandler (Setversionhandler));
}
Upgrade the settings data to the latest version, based on the current version number and the version number that will be set
Async void Setversionhandler (Setversionrequest request)
{
Asynchronous operations
Setversiondeferral Deferral = Request. Getdeferral ();
Await Dispatcher.runasync (Coredispatcherpriority.normal, () =>
{
Lblmsg.text = "CurrentVersion:" + request. CurrentVersion; Current version number
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "Desiredversion:" + request. Desiredversion; The version number that will be set
Lblmsg.text + = Environment.NewLine;
Lblmsg.text + = "ApplicationData.Current.Version:" + ApplicationData.Current.Version; Current version number
});
Complete the asynchronous operation
Deferral.complete ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.