热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> Silverlight>>正文内容

silverlight 独立存储

发布时间:2010年11月25日点击数: 佚名

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

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

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

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

1、申请独立存储空间

  1. /// <summary> 
  2.        /// 申请独立存储空间 
  3.        /// </summary> 
  4.        /// <param name="size"></param> 
  5.        /// <returns></returns> 
  6.        public static bool ApplayStrogeSpace(double size) 
  7.        { 
  8.            try 
  9.            { 
  10.                using (var store = IsolatedStorageFile.GetUserStoreForApplication()) 
  11.                { 
  12.                    Int64 quotSize = Convert.ToInt64(size * 1024); 
  13.                    Int64 curSize = store.AvailableFreeSpace; 
  14.                    if (curSize < quotSize) 
  15.                    { 
  16.                        if (store.IncreaseQuotaTo(quotSize)) 
  17.                        { return true; } 
  18.                        else 
  19.                        { return false; } 
  20.                    } 
  21.                    else 
  22.                    { return true; } 
  23.                } 
  24.            } 
  25.            catch (IsolatedStorageException ex) 
  26.            { throw new IsolatedStorageException("申请独立存储空间失败!" + ex.Message); } 
  27.        } 

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

  1. /// <summary> 
  2.         /// 保存字符串到文件 
  3.         /// </summary> 
  4.         /// <param name="data"></param> 
  5.         /// <param name="fileName"></param> 
  6.         public static void SaveString(string data, string fileName) 
  7.         { 
  8.             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
  9.             { 
  10.                 if (isf.FileExists(fileName)) 
  11.                 { isf.DeleteFile(fileName); } 
  12.  
  13.                 using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Create, isf)) 
  14.                 { 
  15.                     using (var sw = new StreamWriter(isfs)) 
  16.                     { 
  17.                         sw.Write(data); 
  18.                         sw.Close(); 
  19.                     } 
  20.                 } 
  21.             } 
  22.         } 
  23.  
  24.         /// <summary> 
  25.         /// 泛类型支持存储文件 
  26.         /// </summary> 
  27.         /// <typeparam name="T"></typeparam> 
  28.         /// <param name="Tdata"></param> 
  29.         /// <param name="fileName"></param> 
  30.         public static void SaveTData<T>(T Tdata, string fileName) 
  31.         { 
  32.             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
  33.             { 
  34.                 if (isf.FileExists(fileName)) 
  35.                 { 
  36.                     isf.DeleteFile(fileName); 
  37.                 } 
  38.                 IsolatedStorageFileStream isfs = isf.CreateFile(fileName); 
  39.                 DataContractSerializer ser = new DataContractSerializer(typeof(T)); 
  40.                 ser.WriteObject(isfs, Tdata); 
  41.                 isfs.Close(); 
  42.             } 
  43.         } 

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

  1. /// <summary> 
  2.         /// 返回字符串 
  3.         /// </summary> 
  4.         /// <param name="fileName"></param> 
  5.         /// <returns></returns> 
  6.         public static string FindData(string fileName) 
  7.         { 
  8.             string data = string.Empty; 
  9.             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
  10.             { 
  11.                 if (isf.FileExists(fileName)) 
  12.                 { 
  13.                     using (var isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf)) 
  14.                     { 
  15.                         using (var sr = new StreamReader(isfs)) 
  16.                         { 
  17.                             string lineData; 
  18.                             while ((lineData = sr.ReadLine()) != null) { data += lineData; } 
  19.                         } 
  20.                     } 
  21.                 } 
  22.  
  23.             } 
  24.             return data; 
  25.         } 
  26.  
  27.  
  28.         /// <summary> 
  29.         /// 泛类型返回 
  30.         /// </summary> 
  31.         /// <typeparam name="T"></typeparam> 
  32.         /// <param name="fileName"></param> 
  33.         /// <returns></returns> 
  34.         public static T FindTData<T>(string fileName) 
  35.         { 
  36.             T data = default(T); 
  37.             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
  38.             { 
  39.                 if (isf.FileExists(fileName)) 
  40.                 { 
  41.                     IsolatedStorageFileStream isfs = isf.OpenFile(fileName, FileMode.Open); 
  42.                     var ser = new DataContractSerializer(typeof(T)); 
  43.                     data = (T)ser.ReadObject(isfs); 
  44.                     isfs.Close(); 
  45.                 } 
  46.             } 
  47.             return data; 
  48.         } 

4、删除数据

  1. /// <summary> 
  2.         /// 清空独立存储 
  3.         /// </summary> 
  4.         /// <param name="fileName"></param> 
  5.         public static void ReMove(string fileName) 
  6.         { 
  7.             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
  8.             { 
  9.                 if (isf.FileExists(fileName)) 
  10.                 { isf.DeleteFile(fileName); } 
  11.             } 
  12.         } 

这几个处理可以放在一个单独的类中,为其他类提供服务。使用很简单

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作