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