silverlight的独立存储

2018-01-24 15:44:26 浏览数 (1)

这个东西有点象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);
                       }
                   }
                   // 读取文件 MainDir1SubDir1demo.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.";
            }

0 人点赞