Windows Phone 7 independent storage isolated storage

Source: Internet
Author: User
Tags subdomain

Windows Phone 7 supports data access in the following ways: XML, isolated storage [independent storage], and cloud [cloud storage]. Windows Phone 7 does not support local storage.

Database APIs can be used.

Isolated storage [independent storage] can store your data locally in two ways. The first is through the key/value pair in the database, called isolatedstoragesettings. The second is to create a real file and directory called isolatedstoragefile.

(1) isolatedstoragesettings
Isolatedstoragesettings allows you to store key/value pairs in a dictionary (note that no setting is required) and then read them out. This data will be stored all the time, regardless of the applicationProgramStop/start, or shut down. It always exists unless you delete it or the user detaches your application. One thing to remember is that you cannot read it before it is added to the dictionary.

Isolatedstoragesettings provides a convenient way to store user-specific data as key/value pairs in the local isolatedstoragefile. A typical purpose is to save settings, such as the number of images displayed on each page and page layout options.

User settings can be specific to an application or shared between multiple applications in the same domain. Applicationsettings stores the settings for each application, each computer, and each user. The range is determined by the complete path of the application. xap file. Sitesettings stores the settings for each domain, each computer, and each user. The range is determined by the subdomain that carries the application. xap file. For example, an application located in the http://www.contoso.com/site1/application.xap will have different applicationsettings than an application located in the http://www.contoso.com/site2/application.xap. However, the two applications will share the same sitesettings because they are hosted in the same subdomain.

Using system. Io. isolatedstorage;

Isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings;
// Use the applicationsettings attribute to create a new instance for storing the dictionary of key/value pairs in the independent storage.
// Applicationsettings is specific to a user and an application. The application scope is determined by the complete path of the application.

private void initializesettings ()
{< br> If (settings. contains ("emailflag")
{< br> emailflag. ischecked = (bool) settings ["emailflag"];
}< br> else settings. add ("emailflag", false);
}

Private void emailflag_unchecked (Object sender, routedeventargs E)
{
Settings ["emailflag"] = false;
}

Private void emailflag_checked (Object sender, routedeventargs E)
{
Settings ["emailflag"] = true;
}
}

(2) isolatedstoragefile

Isolatedstoragefile indicates an independent storage zone that contains files and directories. Using isolatedstoragefile is a mechanism that allows you to store real files on your device.

This class abstracts virtual file systems that are stored independently. The isolatedstoragefile object corresponds to a specific independent storage range, in which there is a file represented by the isolatedstoragefilestream object. Applications can use independent storage to store data in the independent parts of the data in the file system, without specifying a specific path in the file system.

The root of the Virtual File System is located in the fuzzy folder of each user on the physical file system. Each unique identifier provided by the host is mapped to a different root that provides each application with its own virtual file system. An application cannot navigate from its own file system to another file system.

Because the independent storage zone is within the scope of a specific assembly, most other hostsCodeYou cannot access the data of your code (highly trusted managed code and management tools can access the storage zone from other assemblies ). Unmanaged code can access any independent storage area.

Example:
Create a text file in a sub-directory and read the content in the file. You can also create and delete directories, subdirectories, and files. Create a new isolatedstoragefile object and write it to the drive using an isolatedstoragefilestream object.

Using system. Io. isolatedstorage;
Using system. IO;

Private void savebutton_click (Object sender, routedeventargs E)
{
// Obtain a virtual local storage for the program
Isolatedstoragefile filestorage = isolatedstoragefile. getuserstoreforapplication ();

// Create a new folder
Filestorage. createdirectory ("textfiles ");

// Create a TXT file stream
Streamwriter filewriter = new streamwriter (New isolatedstoragefilestream ("textfiles \ newtext.txt", filemode. openorcreate, filestorage ));
// Write content to the file
Filewriter. writeline (writetext. Text );
// Close streamwriter.
Filewriter. Close ();
}

Private void getbutton_click (Object sender, routedeventargs E)
{
// Obtain a virtual local storage for the program
Isolatedstoragefile filestorage = isolatedstoragefile. getuserstoreforapplication ();
// Create a new streamreader
Streamreader filereader = NULL;

Try
{
// Read the file
Filereader = new streamreader (New isolatedstoragefilestream ("textfiles \ newtext.txt", filemode. Open, filestorage ));
// Read content
String textfile = filereader. Readline ();

Viewtext. Text = textfile;
Filereader. Close ();
}
Catch
{
Viewtext. Text = "need to create directory and the file first .";
}
}

 

Related Article

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.