Silverlight-管理独立存储(Isolated Storage)
Silverlight中的独立存储是其内部的可信任的可访问文件空间,在这里你可以使用Silverlight 随意的创建、读取、写入、删除目录和文件,它有一些类似于Cookie,但是它可以在客户端保存大量的数据。这个空间默认是1M,如果不够的时候可以申请 扩大容量。
网站+用户+应用程序定位一个独立存储,也就是说必须得相同网站,相同用户,相同应用程序才能够访问这个独立的存储空间。独立存 储是IsolatedStorageFile密封类来进行设置的,这个类分布在命名空间System.IO.IsolatedStorag。我们引用 System.IO命名空间对文件进行操作。下面我们来看一个演示的Xaml代码如下:
| <Grid x:Name="LayoutRoot" Background="White"> |
| <Button Content="设置独立存储" Height="23" HorizontalAlignment="Left" Margin="29,79,0,0" |
| Name="btnSetStorage" VerticalAlignment="Top" Width="75" Click="btnSetStorage_Click" /> |
| <Button Content="清空独立存储" Height="23" HorizontalAlignment="Left" Margin="268,79,0,0" |
| Name="btnClearStorage" VerticalAlignment="Top" Width="75" Click="btnClearStorage_Click" /> |
| <Button Content="获取独立存储列表" Height="23" HorizontalAlignment="Left" Margin="142,79,0,0" |
| Name="btnGetStorage" VerticalAlignment="Top" Width="107" Click="btnGetStorage_Click" /> |
| <ListBox Height="165" HorizontalAlignment="Left" Margin="12,123,0,0" Name="listBox1" |
| VerticalAlignment="Top" Width="166" /> |
| <ListBox Height="165" HorizontalAlignment="Left" Margin="198,123,0,0" Name="listBox2" |
| VerticalAlignment="Top" Width="166" /> |
| <Button Content="读取" Height="23" HorizontalAlignment="Left" Margin="365,79,0,0" |
| Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> |
| <sdk:Label Height="28" HorizontalAlignment="Left" Margin="370,123,0,0" Name="label1" |
| VerticalAlignment="Top" Width="189" /> |
| <sdk:Label Height="28" HorizontalAlignment="Left" Margin="370,181,0,0" Name="label2" |
| VerticalAlignment="Top" Width="189" /> |
| </Grid> |
然后我们来看Xaml.cs代码中使用IsolatedStorageFile对独立存储进行添加目录,添加文件,读取文件,删除文件及目录,扩展独立存储空间等操作。
| public partial class MainPage : UserControl |
| { |
| public MainPage() |
| { |
| InitializeComponent(); |
| } |
| private void btnSetStorage_Click(object sender, RoutedEventArgs e) |
| { |
| using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) |
| { |
| //创建First父目录 |
| if(!storage.DirectoryExists("FatherFirstDir")) |
| { |
| storage.CreateDirectory("FatherFirstDir"); |
| //创建子目录 |
| string SonDir = Path.Combine("FatherFirstDir", "SonFirstDir"); |
| storage.CreateDirectory(SonDir); |
| //创建文件 |
| IsolatedStorageFileStream fileStream = storage.CreateFile(Path.Combine(SonDir, "First.txt")); |
| using (StreamWriter swriter = new StreamWriter(fileStream)) |
| { |
| swriter.Write("这是第一个程序txt"); |
| } |
| fileStream.Close(); |
| } |
| //创建Secend父目录 |
| if (!storage.DirectoryExists("FatherSecendDir")) |
| { |
| storage.CreateDirectory("FatherSecendDir"); |
| //在一级目录下添加一个文件 |
| IsolatedStorageFileStream fileStream = storage.CreateFile(Path.Combine("FatherSecendDir", "second.txt")); |
| using (StreamWriter swriter = new StreamWriter(fileStream)) |
| { |
| swriter.Write("新的txt程序"); |
| } |
| fileStream.Close(); |
| } |
| //当前的独立存储状态 |
| this.label1.Content = "最大空间量:" + storage.Quota + " 已使用量:" + storage.UsedSize; |
| //获取文件First.txt的值 |
| using (StreamReader reader = new StreamReader(storage.OpenFile("FatherFirstDir\\SonFirstDir\\First.txt", |
| FileMode.Open, FileAccess.Read))) |
| { |
| this.label2.Content = reader.ReadToEnd(); |
| } |
| } |
| } |
| private void btnGetStorage_Click(object sender, RoutedEventArgs e) |
| { |
| //获取文件夹中的文件以及所有的文件夹名称 |
| using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) |
| { |
| if (storage.DirectoryExists("FatherSecendDir")) |
| { |
| String[] fileList = storage.GetFileNames("FatherSecendDir/"); |
| this.listBox1.ItemsSource = fileList; |
| String[] dirList = storage.GetDirectoryNames("*"); |
| this.listBox2.ItemsSource = dirList; |
| } |
| } |
| } |
| private void btnClearStorage_Click(object sender, RoutedEventArgs e) |
| { |
| //删除所有的文件以及文件夹 |
| using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) |
| { |
| //在这里简单做一个判断,实际应用过程中不可 |
| if (storage.FileExists("FatherFirstDir\\SonFirstDir\\First.txt")) |
| { |
| storage.DeleteFile("FatherFirstDir\\SonFirstDir\\First.txt"); |
| storage.DeleteDirectory("FatherFirstDir\\SonFirstDir"); |
| storage.DeleteDirectory("FatherFirstDir"); |
| storage.DeleteFile("FatherSecendDir\\second.txt"); |
| storage.DeleteDirectory("FatherSecendDir"); |
| } |
| } |
| } |
| private void button1_Click(object sender, RoutedEventArgs e) |
| { |
| int addSpaceSize = 2097152; |
| //增加最大独立存储空间量 |
| using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) |
| { |
| if (storage.AvailableFreeSpace < addSpaceSize) |
| { |
| storage.IncreaseQuotaTo(storage.Quota+ addSpaceSize); |
| } |
| this.label1.Content = "最大空间量:" + storage.Quota + " 已使用量:" + storage.UsedSize; |
| } |
| } |
| } |
现 在我们来看看如何去看独立存储中的文件夹以及文件,在下图位置设置断点,然后调试,先点击"设置独立存储",然后点击"获取独立存储",然后安装下面去找 到m_AppFilesPath字段的值,复制这个值到Windows文件夹的地址栏,按下确定键即可进入独立存储空间的目录下。在这里你可以看到以下的 独立存储文件夹。



Silverlight-管理独立存储(Isolated Storage)的更多相关文章
- C Primer Plus--C存储类、链接和内存管理之存储类(storage class)
目录 存储类 作用域 链接 存储时期 自动变量 寄存器变量 具有代码块作用域的静态变量 具有外部链接的静态变量 extern关键字 具有内部链接的静态变量 多文件 存储类 C为变量提供了5种不同的存储 ...
- 与众不同 windows phone (6) - Isolated Storage(独立存储)
原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...
- Silverlight 独立存储(IsolatedStorageFile)
1.在Web中添加天气服务引用地址 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 2.在Web中添加Wcf服务接口I ...
- Atitit usrqbg1821 Tls 线程本地存储(ThreadLocal Storage 规范标准化草案解决方案ThreadStatic
Atitit usrqbg1821 Tls 线程本地存储(ThreadLocal Storage 规范标准化草案解决方案ThreadStatic 1.1. ThreadLocal 设计模式1 1.2. ...
- Windows Server 2016软件定义存储:Storage Spaces Direct的关键特性
[TechTarget中国原创] 微软在Windows Server 2016 Technical Preview 2中引入了Storage Spaces Direct.这个特性将本地存储扩展为高可用 ...
- 利用KeyVault来加强存储Azure Storage访问密钥管理
很多时候管理Azure的存储账号我们都需要通过下面的界面管理访问密钥,大部分情况下通过密钥的轮替使用更新就可以做到安全管理了. 但是很多时候存储账号的Key就会不小心分发到开人员.测试人员.和管理员手 ...
- win10的独立存储
win10的独立存储和win8的大致相同 Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.Appl ...
- 开放系统的直连式存储(Direct-Attached Storage,简称DAS)
开放系统的直连式存储(Direct-Attached Storage,简称DAS)已经有近四十年的使用历史,随着用户数据的不断增长,尤其是数百GB以上时,其在备份.恢复.扩展.灾备等方面的问题变得日益 ...
- HTML5本地存储(Local Storage) 的前世今生
长久以来本地存储能力一直是桌面应用区别于Web应用的一个主要优势.对于桌面应用(或者原生应用),操作系统一般都提供了一个抽象层用来帮助应用程序保存其本地数据 例如(用户配置信息或者运行时状态等). 常 ...
随机推荐
- ListView(下)自定义适配器
(一) 1.效果图 2.activity_main.xml <?xml version="1.0" encoding="utf-8"?> <L ...
- python列表和分片
列表的分片 str = " print("打印第0个元素:" ,str[0]) print("负数表示倒数第N个元素,-1表示倒数第一个元素:" ,s ...
- 如何设断点????-----使用WinDbg调试SQL Server查询
http://www.cnblogs.com/woodytu/p/4665427.html http://www.sqlservercentral.com/blogs/aschenbrenner/20 ...
- iOS开发笔记_5.线程,HTTP请求,定时器
说起线程,不会陌生了,操作系统课程里已经详细介绍了这个东东,这里就不解释了,想要了解的问问百度或者翻翻书. 线程的创建 总结了昨天的学习,有下面几种创建的方式. //第一种 NSThread *t = ...
- windows SFC(System File Checker) 命令的使用
SFC(System File Checker)可以扫描所有受保护的系统文件的完整性,并使用正确的 Microsoft 版本替换. 步骤:点击开始,输入cmd: 右键,以管理员身份运行 输入sfc/s ...
- Lucene的学习及使用实验
实验一下Lucene是怎么使用的. 参考:http://www.importnew.com/12715.html (例子比较简单) http://www.yiibai.com/lucene/lucen ...
- Centos6.5搭建RHCS集群-实现GFS+iSCSI网络共享存储
RHCS集群配置 需要用到的各组件功能:RHCS(Red Hat Cluster Suite):能够提供高可用性.高可靠性.负载均衡.存储共享且经济廉价的集群工具集合.LUCI:是一个基于web的集群 ...
- [Python爬虫] 之十七:Selenium +phantomjs 利用 pyquery抓取梅花网数据
一.介绍 本例子用Selenium +phantomjs爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字: ...
- jstack来分析。当linux出现cpu被java程序消耗过高时
我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2 18 ...
- 【招聘App】—— React/Nodejs/MongoDB全栈项目:socket.io&聊天实现
前言:最近在学习Redux+react+Router+Nodejs全栈开发高级课程,这里对实践过程作个记录,方便自己和大家翻阅.最终成果github地址:https://github.com/66We ...