下面来看各个功能的实现:
创建目录,直接使用CreateDirectory方法就可以了,另外还可以使用DirectoryExistes方法来判断目录是否已经存在:
void btnCreateDirectory_Click(object sender, RoutedEventArgs e){ using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String directoryName = this.txtDirectoryName.Text; if (this.lstDirectories.SelectedItem != null) { directoryName = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), directoryName); } if (!store.DirectoryExists(directoryName)) { store.CreateDirectory(directoryName); HtmlPage.Window.Alert("创建目录成功!"); } }}
创建文件,通过CreateFile方法来获取一个IsolatedStorageFileStream,并将内容写入到文件中:
void btnCreateFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem == null && this.txtDirectoryName.Text == "") { HtmlPage.Window.Alert("请先选择一个目录或者输入目录名"); return; } using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath; if (this.lstDirectories.SelectedItem == null) { filePath = System.IO.Path.Combine(this.txtDirectoryName.Text, this.txtFileName.Text + ".txt"); } else { filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.txtFileName.Text + ".txt"); } IsolatedStorageFileStream fileStream = store.CreateFile(filePath); using (StreamWriter sw = new StreamWriter(fileStream)) { sw.WriteLine(this.txtFileContent.Text); } fileStream.Close(); HtmlPage.Window.Alert("写入文件成功!"); }}
读取文件,直接使用System.IO命名空间下的StreamReader:
void btnReadFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem == null || this.lstFiles.SelectedItem == null) { HtmlPage.Window.Alert("请先选择目录和文件!"); return; } using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.lstFiles.SelectedItem.ToString()); if (store.FileExists(filePath)) { StreamReader reader = new StreamReader(store.OpenFile(filePath, FileMode.Open, FileAccess.Read)); this.txtFileContent.Text = reader.ReadToEnd(); this.txtDirectoryName.Text = this.lstDirectories.SelectedItem.ToString(); this.txtFileName.Text = this.lstFiles.SelectedItem.ToString(); } }}
删除目录和文件:
void btnDeleteFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem != null && this.lstFiles.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.lstFiles.SelectedItem.ToString()); store.DeleteFile(filePath); HtmlPage.Window.Alert("删除文件成功!"); } }}void btnDeleteDirectory_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { store.DeleteDirectory(this.lstDirectories.SelectedItem.ToString()); HtmlPage.Window.Alert("删除目录成功!"); } }}
获取目录列表和文件列表:
void lstDirectories_SelectionChanged(object sender, SelectionChangedEventArgs e){ if (lstDirectories.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String[] files = store.GetFileNames( this.lstDirectories.SelectedItem.ToString() + "/"); this.lstFiles.ItemsSource = files; } }}void BindDirectories(){ using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String[] directories = store.GetDirectoryNames("*"); this.lstDirectories.ItemsSource = directories; }}
在本文一开始我就提到独立存储严格的限制了应用程序可以存储的数据的大小,但是我们可以通过IsolatedStorageFile类提供的IncreaseQuotaTo方法来申请更大的存储空间,空间的大小是用字节作为单位来表示的,如下代码片段所示,申请独立存储空间增加到5M:
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()){ long newQuetaSize = 5242880; long curAvail = store.AvailableFreeSpace; if (curAvail < newQuetaSize) { store.IncreaseQuotaTo(newQuetaSize); }}
当我们试图增加空间大小时浏览器将会弹出一个确认对话框,供我们确认是否允许增加独立存储的空间大小
文件被存往何处
既然独立独立存储是存放在客户端本地,那到底存放在何处呢?在我个人计算机上的地址为:C:\Users\TerryLee\AppData\LocalLow\Microsoft\Silverlight\is\035kq51b.2q4\pksdhgue.3rx\1,不同机器会有一些变化,另外在XP下的存储位置与Vista是不相同的。在g文件夹下面,我们找到当前应用程序的一些公有信息,可以看到有如下三个文件:
id.dat记录了当前应用程序的ID
quota.dat记录了当前应用程序独立存储的配额,即存储空间大小
used.dat记录已经使用的空间
在另一个s文件夹下可以找到我们创建的目录以及文件,并且可以打开文件来看到存储的内容,如下图所示:
禁用独立存储
现在我们来思考一个问题,既然独立存储是一个与Cookie机制类似的局部信任机制,我们是否也可以禁用独立存储呢?答案自然是肯定的。在Silverlight应用程序上点击右键时,选择Silverlight Configuration菜单,将会看到如下窗口:
在这里我们可以看到每一个应用程序存储空间的大小以及当前使用的空间;可以删除应用程序独立存储数据或者禁用独立存储的功能。
独立存储配置
最后在简单说一下独立存储配置,在Beta 1时代是应用程序配置,现在不仅支持应用程序配置,同时还支持站点配置,我们可以用它来存储应用程序配置如每个页面显示的图片数量,页面布局自定义配置等等,使用IsolatedStorageSettings类来实现,该类在设计时使用了字典来存储名-值对,它的使用相当简单:
IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;appSettings.Add("mykey","myValue");appSettings.Save();IsolatedStorageSettings siteSettings = IsolatedStorageSettings.SiteSettings;siteSettings.Add("mykey1","myValue1");siteSettings.Save();
独立存储配置的机制与我们上面讲的一样,它也是基于本地文件存储,系统默认的会创建一个名为__LocalSettings的文件进行存储,如下图所示:
打开文件后可以看到,存储的内容(此处进行了整理)
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <KeyValueOfstringanyType> <Key>mykeyKey> <Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:string">myValueValue> KeyValueOfstringanyType>ArrayOfKeyValueOfstringanyType>
值得一提的是使用独立存储配置不仅仅可以存储简单类型的数据,也可以存储我们自定义类型的数据。
小结
本文详细介绍了Silverlight 2中的独立存储机制,希望对大家有所帮助。