前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >silverlight的独立存储

silverlight的独立存储

作者头像
菩提树下的杨过
发布2018-01-24 15:44:26
9040
发布2018-01-24 15:44:26
举报
文章被收录于专栏:菩提树下的杨过

这个东西有点象Flash的Cookie,可以用来在客户端存储一些数据,我在官方文档上读到这个功能的第一反应就是:用它来做IM的客户端聊天记录存储太棒了,呵呵

这里把官方文档上的示例精减整理了一下,贴在这里纪念

先引用 using System.IO.IsolatedStorage; using System.IO;

下面的代码展示了,如何在存储区创建目录/文件,以及如何写入文件,读取文件

代码语言:js
复制
1try
           {
               using (var store = IsolatedStorageFile.GetUserStoreForApplication())
               {
                   
                   StringBuilder sb = new StringBuilder();
                  //创建主目录
                   store.CreateDirectory("MainDir1");

                   //在MainDir1下创建子目录SubDir1
                   string subdirectory1 = Path.Combine("MainDir1", "SubDir1");
                   store.CreateDirectory(subdirectory1);

                   //在SubDir1目录下创建文本文件demo.txt
                   IsolatedStorageFileStream subDirFile = store.CreateFile(Path.Combine(subdirectory1, "demo.txt"));
                   subDirFile.Close();
                   string filePath = Path.Combine(subdirectory1, "demo.txt");
                   if (store.FileExists(filePath))
                   {
                       try
                       {
                           using (StreamWriter sw = new StreamWriter(store.OpenFile(filePath, FileMode.Open, FileAccess.Write)))
                           {
                               sw.WriteLine("To do list:");
                               sw.WriteLine("1. Buy supplies.");
                           }
                       }
                       catch (IsolatedStorageException ex)
                       {
                           sb.AppendLine(ex.Message);
                       }
                   }
                   // 读取文件 MainDir1\SubDir1\demo.txt
                   try
                   {
                       using (StreamReader reader = new StreamReader(store.OpenFile(filePath,FileMode.Open, FileAccess.Read)))
                       {
                           string contents = reader.ReadToEnd();
                           sb.AppendLine(filePath + " contents:");
                           sb.AppendLine(contents);
                       }
                   }
                   catch (IsolatedStorageException ex)
                   {
                       sb.AppendLine(ex.Message);
                   }
                   //删除文件
                   try
                   {
                       if (store.FileExists(filePath))
                       {
                           store.DeleteFile(filePath);
                       }
                   }
                   catch (IsolatedStorageException ex)
                   {
                       sb.AppendLine(ex.Message);
                   }

                   //移除store
                   store.Remove();
                   sb.AppendLine("Store removed.");
                   txtParam.Text = sb.ToString();
               }
           }
           catch (IsolatedStorageException ex)
           {
               txtParam.Text = ex.Message.ToString();
           }

存储区的默认大小是100K,如果感觉不够用了,可以用下面的代码向用户申请扩大:

代码语言:js
复制
// Obtain an isolated store for an application.
           try
           {
               using (var store = IsolatedStorageFile.GetUserStoreForApplication())
               {
                   // Request 5MB more space in bytes.
                   Int64 spaceToAdd = 52428800;
                   Int64 curAvail = store.AvailableFreeSpace;
                   // If available space is less than
                   // what is requested, try to increase.
                   if (curAvail < spaceToAdd)
                   {
                       // Request more quota space.
                       if (!store.IncreaseQuotaTo(store.Quota + spaceToAdd))
                       {
                           // The user clicked NO to the
                           // host's prompt to approve the quota increase.
                       }
                       else
                       {
                           // The user clicked YES to the
                           // host's prompt to approve the quota increase.
                       }
                   }
               }
           }
           catch (IsolatedStorageException)
           {
               // TODO: Handle that store could not be accessed.
          }

想知道存储区的空间使用情况吗?

代码语言:js
复制
// Obtain an isolated store for an application.
           try
           {
               using (var store = IsolatedStorageFile.GetUserStoreForApplication())
               {
                   string spaceUsed = (store.Quota - store.AvailableFreeSpace).ToString();
                   string spaceAvailable = store.AvailableFreeSpace.ToString();
                   string curQuota = store.Quota.ToString();
                   txtParam.Text =
                       String.Format("Quota: {0} bytes, Used: {1} bytes, Available: {2} bytes",
                               curQuota, spaceUsed, spaceAvailable);
               }
           }
            catch (IsolatedStorageException)
           {
               txtParam.Text = "Unable to access store.";
            }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009-08-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档