windows phone7 学习笔记09——隔离存储空间(IsolatedStorage)

  windows phone的所有文件IO操作都被限制在隔离存储空间中(IsolatedStorage),因此一个应用程序是不能访问注册表和其他应用程序内容的。虽然限制很多,但这样也对手机安全和规范起到了很好的作用。

  WP7中的隔离存储空间是没有大小限制的,可以无限制的使用空间,但最好能把数据同步到云端,减少本地存储。

  我们在隔离存储空间中可以增加、删除、修改文件和目录,也可以在隔离存储空间中存储程序配置的信息。

 

  隔离存储空间用到3个重要的类:

  IsolatedStorageFile:用来操控隔离存储空间里面的目录以及文件;

  IsolatedStorageFileStream:用于读写隔离存储空间里面的文件流;

  IsolatedStorageSettings:用于存储配置信息的Dictionary。

 

  首先如果我们要使用隔离存储空间需要引用两个命名空间:

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

 

  1、目录操作

  (1)新增目录

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.DirectoryExists(folderName))
{
MessageBox.Show("已经存在目录" + folderName+ ",无法新建!");
}
else
{
isoFile.CreateDirectory(folderName);
MessageBox.Show("新建成功!");
}
}

  (2)检查目录是否存在

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if(isoFile.DirectoryExists(folderName))
{
MessageBox.Show("存在目录" + folderName);
}
else
{
MessageBox.Show("不存在目录" + folderName);
}
}


  (3)删除目录

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.DirectoryExists(folderName))
{
isoFile.DeleteDirectory(folderName);
MessageBox.Show(folderName+ "已成功删除!");
}
else
{
MessageBox.Show("不存在目录" + folderName);
}
}


  2、文件操作

  (1)新增文件

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


  (2)检查文件是否存在

            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoFile.FileExists(fileName))
{
MessageBox.Show("存在文件" + fileName);
}
else
{
MessageBox.Show("不存在文件" + fileName);
}
}


  (3)删除文件

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


  3、文件读写

  (1)写文件

            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)读文件

            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、通过IsolatedStorageSettings类来存储和读取配置

  (1)写配置

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


  (2)读配置

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


  扩展阅读:http://www.cnblogs.com/zdave/archive/2011/06/01/2067282.html

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

posted @ 2012-02-18 17:55  zhangkai2237  阅读(727)  评论(0编辑  收藏  举报