Currently, data stored in windwos Phone 7 must be written to an independent storage space. There are two storage methods: 1. Independent file storage; 2. Independent storage.
(1) Independent file storage
The usage of independent file storage is similar to that of files.
Write:
// Obtain the virtual store for applicationisolatedstoragefile mystore = isolatedstoragefile. getuserstoreforapplication (); // create a new folder and call it "imagefolder" mystore. createdirectory ("imagefolder"); // create a new file and assign a streamwriter to the store and this new file (myfile.txt) // also take the text contents from the txtwrite control and write it to myfile.txt streamwriter writefile = new streamwriter (New isolatedstoragefilestream ("imagefolder \ myfile.txt", filemode. openorcreate, mystore); writefile. writeline (txtwrite. text); writefile. close ();
Read:
// Obtain a virtual store for application isolatedstoragefile mystore = isolatedstoragefile. getuserstoreforapplication (); // this code will open and read the contents of myfile.txt // Add exception in case the user attempts to click "read button first. streamreader readfile = NULL; try {readfile = new streamreader (New isolatedstoragefilestream ("imagefolder \ myfile.txt", filemode. open, mystore); string filetext = readfile. readline (); // The control txtread will display the text entered in the file txtread. TEXT = filetext; readfile. close ();} catch {txtread. TEXT = "need to create directory and the file first. ";}
(2) Set storage independently
// Obtain the storage object isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings; // write data setting ["name"] = "bill"; setting ["Age"] = "20; setting. save (); // read data string name = Setting ["name"];
For more specific examples, refer to the example of Microsoft settingssample, which provides detailed instructions on how to configure storage independently.