Silverlight Isolated Storage 独立存储

刺骨的言语ヽ痛彻心扉 2022-05-15 19:38 239阅读 0赞

Silverlight 独立存储 好比Cookie一样,可以在客户端存储信息,但是他更加强大,独立存储提供了客户端指定目录下的读写权限,可以任意的向其中添加删除修改读取文件。

独立存储将文件存储在系统盘-当前用户-本地的-指定文件夹当中。

独立存储有两个作用域 应用程序级别和站点级别 他就像是一个为Silverlight专门提供的文件夹,用来存放Silverlight的文件信息,比如XML、TXT、Dat、Html等,格式不限只要对你有用。

#

基础操作语法

  1. using System.IO.IsolatedStorage;
  2. using System.IO;
  3. void CreateDir(string dirName)
  4. {
  5. IsolatedStorageFile storeFile =
  6. IsolatedStorageFile.GetUserStoreForApplication();
  7. storeFile.CreateDirectory(dirName);
  8. }
  9. void SaveFile(string savePath, string content)
  10. {
  11. IsolatedStorageFile storeFile =
  12. IsolatedStorageFile.GetUserStoreForApplication();
  13. IsolatedStorageFileStream sf = storeFile.CreateFile(savePath);
  14. using (StreamWriter sw = new StreamWriter(sf))
  15. {
  16. sw.WriteLine(content);
  17. }
  18. sf.Close();
  19. }
  20. void LoadFile(string readPath)
  21. {
  22. string content = string.Empty;
  23. using (IsolatedStorageFile storeFile =
  24. IsolatedStorageFile.GetUserStoreForApplication())
  25. {
  26. if (storeFile.FileExists(readPath))
  27. {
  28. StreamReader sr =
  29. new StreamReader(storeFile.OpenFile
  30. (readPath, FileMode.Open, FileAccess.Read));
  31. content = sr.ReadToEnd();
  32. }
  33. }
  34. }
  35. void DeleteFile(string path)
  36. {
  37. using (IsolatedStorageFile storeFile =
  38. IsolatedStorageFile.GetUserStoreForApplication())
  39. {
  40. storeFile.DeleteFile(path);
  41. }
  42. }
  43. void DeleteDir(string dirPath)
  44. {
  45. using (IsolatedStorageFile storeFile =
  46. IsolatedStorageFile.GetUserStoreForApplication())
  47. {
  48. storeFile.DeleteDirectory(dirPath);
  49. }
  50. }
  51. void LoadDirs()
  52. {
  53. using (IsolatedStorageFile storeFile =
  54. IsolatedStorageFile.GetUserStoreForApplication())
  55. {
  56. var itemSource = storeFile.GetDirectoryNames("*");
  57. }
  58. }

名值对方式存储读取

  1. 这种方式就很像Cookie
  2. string ReadSettings(string key)
  3. {
  4. IsolatedStorageSettings settings =
  5. IsolatedStorageSettings.ApplicationSettings;
  6. return settings[key].ToString();
  7. }
  8. void SaveSettings(string key, string value)
  9. {
  10. IsolatedStorageSettings settings =
  11. IsolatedStorageSettings.ApplicationSettings;
  12. settings.Add(key, value);
  13. settings.Save();
  14. }
  15. void ClearSettings()
  16. {
  17. IsolatedStorageSettings settings =
  18. IsolatedStorageSettings.ApplicationSettings;
  19. settings.Clear();
  20. }

独立存储的文件与名值对分别有两个示例,可以在目录地址链接下载代码阅读。

独立存储的空间大小

独立存储默认的空间上限是1M,可以通过代码设置让这个上限加大。代码如下

  1. //使1用?应|用?程ì序ò存?储¢创′建¨对?象ó
  2. using (IsolatedStorageFile storeFile =
  3. IsolatedStorageFile.GetUserStoreForApplication())
  4. {
  5. //获?取?旧é空?间?大ó小?
  6. long oldSize = storeFile.AvailableFreeSpace;
  7. //定¨义?新?增?空?间?大ó小?
  8. long newSize = 2097152;
  9. if (oldSize < newSize)
  10. {
  11. //分?配?新?的?存?储¢空?间?
  12. storeFile.IncreaseQuotaTo(newSize);
  13. }
  14. }

客户可以通过邮件Silverlight 控件选择Silverlight配置中 ->应用程序存储选项卡 中查看本地有存储了那些Silverlight应用存储信息。

image

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读