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 @   zhangkai2237  阅读(763)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示