Windows phone7 Study Notes 09-isolated storage (isolatedstorage)

Source: Internet
Author: User

All file I/O operations on Windows Phone are restricted in the isolated storage space (isolatedstorage), so an application cannot access the Registry and other application content. Although there are many restrictions, this also plays a very good role in cell phone security and specifications.

There is no size limit on the isolated storage space in WP7, and unlimited space can be used, but it is best to synchronize data to the cloud to reduce local storage.

You can add, delete, and modify files and directories in the isolated bucket, or store program configuration information in the isolated bucket.

 

The isolated bucket uses three important classes:

Isolatedstoragefile: used to isolate directories and files in a bucket;

Isolatedstoragefilestream: used to read and write files in the isolated bucket;

Isolatedstoragesettionary: a dictionary used to store configuration information.

 

First, if we want to use an isolated bucket, We need to reference two namespaces:

using System.IO;
using System.IO.IsolatedStorage;

 

  1. Directory operations

(1) Add a directory

Using (isolatedstoragefile isofile = isolatedstoragefile. getuserstoreforapplication ())
{
If (isofile. directoryexists (Foldername ))
{
MessageBox. Show ("existing Directory" + Foldername + ", cannot be created! ");
}
Else
{
Isofile. createdirectory (Foldername );
MessageBox. Show ("created successfully! ");
}
}

(2) check whether the directory exists

Using (isolatedstoragefile isofile = isolatedstoragefile. getuserstoreforapplication ())
{
If (isofile. directoryexists (Foldername ))
{
MessageBox. Show ("existing Directory" + Foldername );
}
Else
{
MessageBox. Show ("the directory does not exist" + Foldername );
}
}


(3) Delete A directory

Using (isolatedstoragefile isofile = isolatedstoragefile. getuserstoreforapplication ())
{
If (isofile. directoryexists (Foldername ))
{
Isofile. deletedirectory (Foldername );
MessageBox. Show (Foldername + "deleted successfully! ");
}
Else
{
MessageBox. Show ("the directory does not exist" + Foldername );
}
}


2. File Operations

(1) Add a file

      using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
FileStream fileStream = isoFile.CreateFile(fileName);
fileStream.Close();
}


(2) check whether the file exists

Using (isolatedstoragefile isofile = isolatedstoragefile. getuserstoreforapplication ())
{
If (isofile. fileexists (filename ))
{
MessageBox. Show ("existing file" + filename );
}
Else
{
MessageBox. Show ("file not found" + filename );
}
}


(3) Delete an object

      using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
isoFile.DeleteFile(fileName);
}


3. file read/write

(1) Writing files

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoFileStream = isoFile.OpenFile(fileName,FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
StreamWriter streamWriter = new StreamWriter(isoFileStream);
streamWriter.WriteLine(ContentTextBox.Text);
streamWriter.Close();//very importent
}
}


(2) reading files

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoFileStream = isoFile.OpenFile(fileName,FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
StreamReader streamReader = new StreamReader(isoFileStream);
ContentTextBox.Text = streamReader.ReadToEnd().ToString();
streamReader.Close();
}
}


4. Use the isolatedstoragesettings class to store and read configurations.

(1) Write Configuration

       IsolatedStorageSettings.ApplicationSettings[settingName] = SettingTextBox.Text;
IsolatedStorageSettings.ApplicationSettings.Save();//very importent.


(2) read Configuration

            if (IsolatedStorageSettings.ApplicationSettings.Contains(settingName))
{
SettingTextBox.Text = IsolatedStorageSettings.ApplicationSettings[settingName] as string;
}


Additional reading: http://www.cnblogs.com/zdave/archive/2011/06/01/2067282.html

Http: // www-congci-com/item/isolatedstorage-wp7-app-data

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.