代码改变世界

silverlight 独立存储

2013-03-15 16:39  suzh  阅读(180)  评论(0编辑  收藏  举报

 独立存储是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); }
            }
        }

 

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

转自:http://www.cnblogs.com/Clivia/archive/2010/11/13/silverlight04.html