silverlight 独立存储


  独立存储是silverlight提供的一个客户端存储,是一种局部信任机制。
独立存储提供了一个虚拟的文件系统的数据流对象,,是基于.net framework中独立存储建立起来的一个子集。


独立存储具有以下特点:
1、基于silverlight的应用程序被分配了属于它子集的存储空间,但是应用程序的程序集在存储空间中是共享的。
2、 IsolatedStorageFileStream 扩展了 FileStream,使用该类可以在独立存储中读取、写入和创建文件。

 

独立存储的功能通过密封类 IsolatedStorageFile 提供,位于命名空间System.IO.IsolatedStorage中,该类抽象了独立存储的虚拟文件系统。创建该类的一个实例,可以对文件或者文件夹进行管理,结合IsolatedStorageFileStream类类管理文件内容。

 

结合使用做了一个关于silverlight的独立存储应用的例子解说如下:
1、申请独立存储空间

?
/// <summary>
        /// 申请独立存储空间
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static bool ApplayStrogeSpace( double size)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    Int64 quotSize = Convert.ToInt64(size * 1024);
                    Int64 curSize = store.AvailableFreeSpace;
                    if (curSize < quotSize)
                    {
                        if (store.IncreaseQuotaTo(quotSize))
                        { return true ; }
                        else
                        { return false ; }
                    }
                    else
                    { return true ; }
                }
            }
            catch (IsolatedStorageException ex)
            { throw new IsolatedStorageException( "申请独立存储空间失败!" + ex.Message); }
        }


2、保存数据(支持引用类型的数据)

?
/// <summary>
         /// 保存字符串到文件
         /// </summary>
         /// <param name="data"></param>
         /// <param name="fileName"></param>
         public static void SaveString( string data, string fileName)
         {
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isf.FileExists(fileName))
                 { isf.DeleteFile(fileName); }
 
                 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf))
                 {
                     using (var sw = new StreamWriter(isfs))
                     {
                         sw.Write(data);
                         sw.Close();
                     }
                 }
             }
         }
 
         /// <summary>
         /// 泛类型支持存储文件
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="Tdata"></param>
         /// <param name="fileName"></param>
         public static void SaveTData<T>(T Tdata, string fileName)
         {
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isf.FileExists(fileName))
                 {
                     isf.DeleteFile(fileName);
                 }
                 IsolatedStorageFileStream isfs = isf.CreateFile(fileName);
                 DataContractSerializer ser = new DataContractSerializer( typeof (T));
                 ser.WriteObject(isfs, Tdata);
                 isfs.Close();
             }
         }

3、提取数据(支持应用类型的数据)

?
/// <summary>
         /// 返回字符串
         /// </summary>
         /// <param name="fileName"></param>
         /// <returns></returns>
         public static string FindData( string fileName)
         {
             string data = string .Empty;
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isf.FileExists(fileName))
                 {
                     using (var isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf))
                     {
                         using (var sr = new StreamReader(isfs))
                         {
                             string lineData;
                             while ((lineData = sr.ReadLine()) != null ) { data += lineData; }
                         }
                     }
                 }
 
             }
             return data;
         }
 
 
         /// <summary>
         /// 泛类型返回
         /// </summary>
         /// <typeparam name="T"></typeparam>
         /// <param name="fileName"></param>
         /// <returns></returns>
         public static T FindTData<T>( string fileName)
         {
             T data = default (T);
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isf.FileExists(fileName))
                 {
                     IsolatedStorageFileStream isfs = isf.OpenFile(fileName, FileMode.Open);
                     var ser = new DataContractSerializer( typeof (T));
                     data = (T)ser.ReadObject(isfs);
                     isfs.Close();
                 }
             }
             return data;
         }


4、删除数据

?
/// <summary>
         /// 清空独立存储
         /// </summary>
         /// <param name="fileName"></param>
         public static void ReMove( string fileName)
         {
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 if (isf.FileExists(fileName))
                 { isf.DeleteFile(fileName); }
             }
         }

 

这几个处理可以放在一个单独的类中,为其他类提供服务。使用很简单。在这里提供一个示例代码:http://files.cnblogs.com/Clivia/IsoLatedStroage.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值