Isolated storage space for Windows Phone 7
- Concept:
All file I/O operations on Windows Phone 7 are restricted in the isolated bucket. You can add, delete, and modify directory files in the isolated bucket, the configuration information of the program can be stored in the isolated bucket. However, the isolated bucket of each application is independent, which is equivalent to a piece of memory of Windows Phone, only the internal part of the application itself can access its internal information, while external (other applications) cannot access it.
2. Directory operations
Two important classes:
Isolatedstoragefile: used to isolate directories and files in the bucket, such as adding, deleting, modifying, and querying.
Isolatedstoragefilestream: used for read/write operations to isolate file streams in a bucket. For example, this class is used when we need to write something to a file.
Isolatedstoragesettionary: a dictionary used to store program configuration information, such as keys and values of an application.
3. Two namespaces must be referenced when an isolated bucket is used:
using System.IO.IsolatedStorage;using System.IO;
Operations on isolated buckets are similar to traditional file I/O operations..
There is no absolute path in the isolated bucket or there is no root directory. For example, on our windows computer, the root directory of a folder is in drive C, drive D, or drive E, however, in Windows Phone 7, there is no root directory, so there is no absolute path. Therefore, the isolated bucket of the application cannot be obtained through the path, but only through getuserstoreforapplication.()Method.
Example:
Using system. io. isolatedstorage; using system. io; private const string Foldername = "temp1"; // defines a constant. Private void newbutton_click (Object sender, routedeventargs e) {using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () // using indicates that resources can be automatically released after the class is used, that is, the dispose () method is called. // Obtain the application's isolated bucket {file. createdirectory (Foldername); // create a folder MessageBox. Show ("created successfully! ") ;}} Private void checkbutton_click (Object sender, routedeventargs e) {using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () {If (file. directoryexists (Foldername) // The directory contains {MessageBox. show (Foldername + "already exists! ");} Else {MessageBox. Show (Foldername +" does not exist! ") ;}} Private void deletebutton_click (Object sender, routedeventargs e) {using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () {If (file. directoryexists (Foldername) {file. deletedirectory (Foldername); // Delete the MessageBox directory. show (Foldername + "File deleted");} else {MessageBox. show ("no file to be deleted ");}}}
4. File Operations
File Operations are similar to directory operations, including addition, deletion, modification, and query operations.
A. Create a file
Using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () {filestream = file. createfile (filename); // create a file filestream. close (); // close the file stream. If it is not closed, an exception occurs in the delete operation. // here, because createfile () returns a file stream, that is, filestream, therefore, we declare a filestream // variable to save this file}
B. delete the file price
Using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () {If (file. fileexists (filename) // check the file {file. deletefile (filename );}}
C. write and read file information (see the following code)
Private void writebutton_click (Object sender, routedeventargs e) {// obtain the application's isolated bucket using (isolatedstoragefile file = isolatedstoragefile. getuserstoreforapplication () {// open the file stream, filemode. openorcreate indicates creating a new file if the file does not exist, // fileaccess. write indicates the write operation using (isolatedstoragefilestream filestream = file. openfile (filename, filemode. openorcreate, fileaccess. write) {streamwriter strw = new streamwriter (filestream); // specify to write information strw to the file stream we open. writeline (msgtextbox. text); // write the text value in msgtextbox to strw. close (); // close file WRITING} private void readbutton_click (Object sender, routedeventargs e) {// obtain the application's isolated bucket using (isolatedstoragefile = isolatedstoragefile. getuserstoreforapplication () {If (file. fileexists (filename) // if the file exists, it can be read {// open the file stream to be read, filemode. open indicates that only files are opened, // fileaccess. read: Using (isolatedstoragefilestream filestream = file. openfile (filename, filemode. open, fileaccess. read) {streamreader strr = new streamreader (filestream); // read the information msgtextbox from the open file stream. TEXT = strr. readtoend (); // read all information about the object strr. close (); // close the read operation }}}}
Summary of reading and writing files:
First, we need to obtain the isolated storage space of the application, and then we need to open the specified file (which is returned in the form of a file stream after opening ), in the process of opening a file, there are several situations and the filemode is used to determine the method in which we want to open the file. Then, you can perform operations on the file and use fileaccess to determine whether to read or write the file.
5. Isolate bucket configuration information
A. Write information to the configuration information of the isolated Bucket
Private void settingswritebutton_click (Object sender, routedeventargs e) {// put settings into the dictionary of applicationsettings, and assign the value of settingstextbox to settings isolatedstorageset.pdf. applicationsettings [settings] = settingstextbox. text; isolatedstoragesettings. applicationsettings. save (); // save information. This step is critical. If you do not save the settings information we just added, it will not be saved to the isolated bucket of the application, it is stored in the memory, so the settings cannot be read. }
B. read information from the configuration information of the isolated Bucket
Private void settingsreadbutton_click (Object sender, routedeventargs e) {// determines whether the set key exists in the dictionary. If yes, the value is assigned to settingstextbox if (isolatedstorageset.pdf. applicationsettings. contains (settings) {// because the dictionary stores objects, You need to convert them to the corresponding data type. Settingstextbox. Text = isolatedstoragesettings. applicationsettings [settings] as string ;}}
Note: The above content is written by reference to the video of Jackie Lin. If you are not familiar with it, you can refer to the video of Jack Lin. It is worth watching.
(All Rights Reserved. For details, refer to the source)