Silverlight之独立存储(10)

来源:互联网 发布:公网ip绑定域名 编辑:程序博客网 时间:2024/04/19 10:18

独立存储

代码下载

1.        独立存储

独立存储是一种数据存储机制,它在代码与保存的数据之间定义了标准化的关联方式,从而提供隔离性和安全性。同时,标准化也提供了其他好处。管理员可以使用旨在操作独立存储的工具来配置文件存储空间、设置安全策略及删除未使用的数据。通过独立存储,代码不再需要使用唯一的路径来指定文件系统中的安全位置,同时可以保护数据免遭只具有独立存储访问权限的其他应用程序的损坏。不再需要指示应用程序的存储区域位置的硬编码信息。

2.       代码实现例子关键代码

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

StringBuilder sb = new StringBuilder();

            //创建文件夹

            store.CreateDirectory("dir1");

            stringsub11 = Path.Combine("dir1", "SubDir11");

            stringsub12 = Path.Combine("dir1", "SubDir12");

            stringsub13 = Path.Combine("dir1", "SubDir13");

            store.CreateDirectory(sub11);

            store.CreateDirectory(sub12);

            store.CreateDirectory(sub13);

 

            store.CreateDirectory("dir2");

            store.CreateDirectory("dir2\\sub21");

            store.CreateDirectory("dir3");

 

            //创建文件

            IsolatedStorageFileStreamf1 = store.CreateFile(Path.Combine(sub11,"InTheRoot1.txt"));

            f1.Close();

            IsolatedStorageFileStreamf2 = store.CreateFile("InTheRoot12.txt");

            f2.Close();

            IsolatedStorageFileStreamf3 = store.CreateFile("InTheRoot13.txt");

            f3.Close();

 

 

 

            //得到文件夹

            string[]dirs = store.GetDirectoryNames();

            string[]subdirs = store.GetDirectoryNames(Path.Combine("dir1","*"));

 

 

            //得到文件

            string[]files = store.GetFileNames();

            stringsearchpath = Path.Combine(sub11, "*.*");

            string[]filesInSubDirs = store.GetFileNames(searchpath);

 

            //写文件

            strings = "InTheRoot12.txt";

            if(store.FileExists(s))

            {

                try

                {

                    using(StreamWriter sw =newStreamWriter(store.OpenFile(s,FileMode.Open,FileAccess.Write)))

                    {

                        sw.WriteLine("滚滚长江东逝水!");

                    }

                }

                catch(IsolatedStorageException ex)

                {

                }

            }

            //读文件

            try

            {

                using(StreamReader reader =newStreamReader(store.OpenFile(s,FileMode.Open,FileAccess.Read)))

                {

                    stringcontents = reader.ReadToEnd();

                    sb.AppendLine(s + "内容:");

                    sb.AppendLine(contents);

                }

            }

            catch(IsolatedStorageException ex)

            {

                sb.AppendLine(ex.Message);

            }

            //删除文件,删除文件夹

            store.DeleteFile(Path.Combine(sub11,"InTheRoot1.txt"));

            store.DeleteDirectory(sub11);

            //store.Remove();

 代码下载

原创粉丝点击