Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1049163
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-26 17:39:25

IsolatedStorage独立存储空间是保存应用程序的一些数据已经配置文件,独立存储空间相对于其他的wp程序是独立的,也就是说每个wp程序都会有自己的独立存储空间,每个wp程序相互之间不能访问;

什么是Isolated Storage?

Isolated Storage又叫做隔离存储空间,Windows Phone 7手机上用来本地存储数据。下图是一个存储应用的文件夹结构图:

IC381787

Isolated Storage用来创建与维护本地存储。WP7上的架构和Windows下的Silverlight类似,所有的读写操作都只限于隔离存储空间并且无法直接访问磁层操作系统的文件系统。这样能够防止非法的访问以及其他应用的破坏,增强安全性。

提示:如果你有两个应用想要共用一个同一个数据,则没法通过本地存储实现。你可以使用web服务等。

提示:WP7下的隔离存储空间没有配额的限制。应用应该只保存必要的数据。当Windows Phone只剩下10%的可用空间,用户会收到一个提醒并可能停止当前应用。对用户来讲这是一个很差的用户体验。

在隔离存储空间下可以进行目录操作、文件操作、应用程序配置信息等。

什么是Isolated Storage部分参考出处: http://www.cnblogs.com/zdave/archive/2011/05/06/2038924.html

  1. 此类表示包含文件和目录的独立存储区

    IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();
    此类的实例化是得到整个程序的独立存储空间
    属性

    表示独立存储的可用空间量 单位为字节

    该值表示独立存储的最大可用空间量 不能超过该值 单位为字节

    方法

    创建目录

    创建文件

    判断是否存在某个目录,删除目录之前可调用该方法

    删除创建的目录

    判断是否存在某个文件,删除文件之前可调用该方法

    删除传进的文件

    得到匹配的目录名称 这里string支持通配符:单字节(“?”)和多字节(“*”)

    得到匹配的文件名称 这里string支持通配符:单字节(“?”)和多字节(“*”)

    获取应用程序级的独立存储空间

    比较重要的方法,增加独立存储空间空间量,但不可超过

    移除独立存储区范围及其所有内容,利用此方法必须先判断文件和目录是否正在使用,如果正在使用会有异常


  2. 此类是文件流,实现对文件的操作

    IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("test\\TEST.txt", System.IO.FileMode.Open, FileAccess.Read, isStore);

    此实例化类是文件进行操作
    属性

    是否可读 默认为true

    是否可检索 默认为true

    是否可写 默认为true

    文件流写入和读取的路径

    设置的流读取超时时间

    设置的写入流超时时间

    方法

    在异步的时候用到,开始读取

    在异步的时候用到,读取结束

    在异步的时候用到,开始写入

    在异步的时候用到,写入结束

    关闭当前流并释放相关资源

    从当前流读取所有字节并写入目标流

    写入单个字节

    读取单个字节

    写入字节块bytes

    读取文件获得字节块bytes

    限制流的长度


  3. 此类是存储一些配置信息,实例化

    var settings = IsolatedStorageSettings.ApplicationSettings;

    //添加内容
    settings.Add("key", object);
    //保存
    settings.Save();
    //获取配置信息
    string value = settings["key"].ToString();
    //out传参获取值
    string value;
    settings.TryGetValue("key", out value);

  4. 初始化界面
    View Code
    x:Class="IsolatedStorageFileStreamApplication.MainPage"
    xmlns=""
    xmlns:x=""
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d=""
    xmlns:mc=""
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">


    "LayoutRoot" Background="Transparent">

    "Auto"/>
    "*"/>



    "TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    "ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
    "PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>



    "ContentPanel" Grid.Row="1" Margin="12,0,12,0">



    "txtShow" Margin="0,100,0,200" Text="显示读取的资料或者状态" HorizontalAlignment="Center" VerticalAlignment="Center">


  5. 删除操作
    View Code
    ///
    /// 删除文件及路径
    ///

    ///
    ///
    private void btnDel_Click(object sender, RoutedEventArgs e)
    {
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
    //判断是否存在
    if (isoStore.FileExists("test\\TEST.txt"))
    {
    //删除文件
    isoStore.DeleteFile("test\\TEST.txt");
    //删除目录
    isoStore.DeleteDirectory("test");
    //完全删除
    isoStore.Remove();
    //释放资源
    isoStore.Dispose();
    //配置信息
    var settings = IsolatedStorageSettings.ApplicationSettings;
    //清空配置信息
    settings.Clear();
    }
    txtShow.Text = "删除完成";

    }

  6. 写入操作
    View Code
    ///
    /// 写操作
    ///

    ///
    ///
    private void btnWrite_Click(object sender, RoutedEventArgs e)
    {
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
    //独立存储空间可用空间量
    long availableSpace = isoStore.AvailableFreeSpace;
    //独立存储空间最大可用空间量
    long quota = isoStore.Quota;
    //独立存储空间的可用量扩充 字节为单位
    // bool dl = isoStore.IncreaseQuotaTo(quota);
    //创建目录
    isoStore.CreateDirectory("test");
    IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test\\TEST.txt", System.IO.FileMode.Create, isoStore);

    byte onlyOneByte = 101;
    isoStream.WriteByte(onlyOneByte);

    isoStream.Close();
    isoStore.Dispose();
    btnRead.IsEnabled = true;
    txtShow.Text="写入完成";
    //存储配置信息
    var settings = IsolatedStorageSettings.ApplicationSettings;
    //添加配置信息
    settings.Add("ip", "192.168.1.1");
    //保存
    settings.Save();

    }

  7. 读取操作
    View Code
    ///
    /// 读操作
    ///

    ///
    ///
    private void btnRead_Click(object sender, RoutedEventArgs e)
    {
    IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();

    if (isStore.FileExists("test\\TEST.txt"))
    {
    IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("test\\TEST.txt", System.IO.FileMode.Open, FileAccess.Read, isStore);
    //获取路径
    string[] directoryName = isStore.GetDirectoryNames("test");
    //获取文件名 搜索模式。 单字符("?")和多字符("*")通配符都受支持
    string[] fileName = isStore.GetFileNames("test\\T*.txt");
    txtShow.Text = "写入资料值为:" + isStream.ReadByte().ToString() + ";\n路径为:" + directoryName[0].ToString() + ";\n文件名称为:" + fileName[0];

    isStream.Close();
    isStore.Dispose();
    txtShow.Text =txtShow.Text+ "\n读取完成";
    //存储配置信息
    var settings = IsolatedStorageSettings.ApplicationSettings;

    //判断key 是否存在
    if (settings.Contains("ip"))
    {
    //只支持key的查询
    string ip = settings["ip"].ToString();
    //out传参获取值
    string ipStr;
    settings.TryGetValue("ip", out ipStr);
    txtShow.Text = txtShow.Text + "\n配置文件ip:" + ip + ";\n out传参ip:"+ipStr;

    }


    }
    }

  8. 读取效果
阅读(1615) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~