Silverlight中独立存储

       这几天都专注于独立存储的学习,TerryLee的文章分析的很详细,笼统的讲,如果忽略一些概念性上的东西,操作独立存储和系统的物理文件系统没有什么区

别,只不过这其中又多了个类似Cookie机制的独立存储配置概念。

      那么专业一点讲,独立存储的API只能进行低信任级别的虚拟文件系统访问,但是它里面提供的File,Directory与我们操作物理文件系统的所提供的API操作方法上

是一致的,对于其使用范围我也找到一段话概括:       捕获      操作目录,文件都是使用的IsolatedStorageFile类,根据作用域的不同,分为了应用程序和站点类型:

      方法签名如下:

         public static IsolatedStorageFile GetUserStoreForApplication() 获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储
         public static IsolatedStorageFile GetUserStoreForSite()  获取由虚拟主机域中的所有应用程序使用的某个用户范围的独立存储。
    不过起初看SDK时并未弄明白这2个方法的区别,后来在MSDN上看英文原文的解释才明白这两者的区别,不知道是我的理论水平太低,还是这部分翻译的比较拗口,
这里本来从方法的命名上可以推敲出来的区别的,比如一个Site下面可能会有多个Application,那么我可以申请一个站点型的独立存储空间,那么这个站点包含的
若干个应用程序就可以共享这个存储空间,这样就不需要为每一个应用程序都单独申请一个空间。
    这两天正好有时间,就将一些基本的操作封装了一下,也便于以后使用,里面还需要以后一步一步的完善           
        public class IsolatedStorageHelper
    {
        private StorageType type;
        public IsolatedStorageFile storeFile;
        public IsolatedStorageHelper(StorageType stroagetype)
        {
            type = stroagetype;
        }
        public IsolatedStorageHelper()
        {
            type = StorageType.App;
        }
        private IsolatedStorageFile GetStoreFileType()
        {
            if (type == StorageType.App)
            {
                storeFile = IsolatedStorageFile.GetUserStoreForApplication();
            }
            else
            {
                storeFile = IsolatedStorageFile.GetUserStoreForSite();
            }
            return storeFile;
        }
     }
        public enum StorageType  { App,Site }

      其实对于目录,文件的操作与与使用System.IO中API操作没什么区别

        public bool DeleteDirInStorage(string path)
        {
            using (IsolatedStorageFile isoFile = this.GetStoreFileType())
            {
                bool IsSuccess = false;
                try
                {                   
                    if (!string.IsNullOrEmpty(path))
                    {
                        //先要判断目录中是否有文件
                          string [] files=isoFile.GetFileNames(path+"/*");
                          string filepath=null; 
                          if (files.Length != 0)
                          {
                              foreach (string name in files)
                              {
                                  filepath= System.IO.Path.Combine(path, name);
                                  isoFile.DeleteFile(filepath);
                              }
                          }
                        isoFile.DeleteDirectory(path);
                        IsSuccess = true;
                    }
                }
                catch (IsolatedStorageException e)
                {
                    return IsSuccess;
                }
                return IsSuccess;
            }
        }

      删除目录需要判断目录中是否包含文件,如果包含,则需要先删除文件再删目录。

        public bool CreateFileInStorage(string filename, string content)
       public bool CreateFileInStorage(string dirpath,string filename,string content)

      创建文件使用了方法的重载,文件在C:\Users\ringgo\AppData\LocalLow\Microsoft\Silverlight\is这个路径的目录下,每一个应用程序都在这里分配了

存储空间。

     一般的会存在3个相关文件夹:id.data:存放App的Id;quota.data:存放存储空间大小;used.data:已经使用的存储空间。                                        

      对于IsolatedStorageSettings 这个部分,SDK上有段解释:位于 http://www.qq.com/site1/application.xap 的应用程序将与位于http://www.qq.com/site2/application.xap 的应用程序具有不同的 ApplicationSettings。但是,这两个应用程序将共享相同的 SiteSettings,

因为它们承载于同一个子域中。对于IsolatedStorageSettings 使用,就是用一个键值对来处理即可,和Cookie一样。

        public bool CreateSettingsInStorage(string key,string value)
        {         
                IsolatedStorageSettings isoSetting = this.GetStoreSettingType();
                try
                {
                    if (!isoSetting.Contains(key))
                    {
                        isoSetting.Add(key, value);
                        isoSetting.Save();
                    }
                    else
                    {
                        isoSetting[key] = value;
                        //如果不调用Sava方法,只是修改的一个副本
                        isoSetting.Save();
                    }
                    return true;
                }
                catch (ArgumentNullException e)
                {
                    return false;
                }              
            }       

    其实这里也就是注意一下Sava()的调用,只有这样才会将改动后的值保存在文件中,其会自动的进行序列化操作。

    关于独立存储的基础知识就暂且整理这么多吧,后面再对这个帮助类进行优化,对于其它的认识在实际运用中再加以总结吧

 

   代码下载:IsolatedStorageHelper.cs

原文地址:https://www.cnblogs.com/626498301/p/1818771.html