|
概述
獨(dú)立存儲(chǔ)(Isolated Storage)是Silverlight 2中提供的一個(gè)客戶端安全的存儲(chǔ),它是一個(gè)與Cookie機(jī)制類似的局部信任機(jī)制。獨(dú)立存儲(chǔ)機(jī)制的APIs 提供了一個(gè)虛擬的文件系統(tǒng)和可以訪問(wèn)這個(gè)虛擬文件系統(tǒng)的數(shù)據(jù)流對(duì)象。Silverlight中的獨(dú)立存儲(chǔ)是基于 .NET Framework中的獨(dú)立存儲(chǔ)來(lái)建立的,所以它僅僅是.NET Framework中獨(dú)立存儲(chǔ)的一個(gè)子集。
Silverlight中的獨(dú)立存儲(chǔ)有以下一些特征:
1.每個(gè)基于Silverlight的應(yīng)用程序都被分配了屬于它自己的一部分存儲(chǔ)空間, 但是應(yīng)用程序中的程序集卻是在存儲(chǔ)空間中共享的。一個(gè)應(yīng)用程序被服務(wù)器賦給了一個(gè)唯一的固定的標(biāo)識(shí)值。基于Silverlight的應(yīng)用程序的虛擬文件系統(tǒng)現(xiàn)在就以一個(gè)標(biāo)識(shí)值的方式來(lái)訪問(wèn)了。這個(gè)標(biāo)識(shí)值必須是一個(gè)常量,這樣每次應(yīng)用程序運(yùn)行時(shí)才可以找到這個(gè)共享的位置。
2.獨(dú)立存儲(chǔ)的APIs 其實(shí)和其它的文件操作APIs類似,比如 File 和 Directory 這些用來(lái)訪問(wèn)和維護(hù)文件或文件夾的類。 它們都是基于FileStream APIs 來(lái)維護(hù)文件的內(nèi)容的。
3.獨(dú)立存儲(chǔ)嚴(yán)格的限制了應(yīng)用程序可以存儲(chǔ)的數(shù)據(jù)的大小,目前的上限是每個(gè)應(yīng)用程序?yàn)? MB。
使用獨(dú)立存儲(chǔ)
Silverlight中的獨(dú)立存儲(chǔ)功能通過(guò)密封類IsolatedStorageFile來(lái)提供,位于命名空間System.IO.IsolatedStorag中,IsolatedStorageFile類抽象了獨(dú)立存儲(chǔ)的虛擬文件系統(tǒng)。創(chuàng)建一個(gè) IsolatedStorageFile 類的實(shí)例,可以使用它對(duì)文件或文件夾進(jìn)行列舉或管理。同樣還可以使用該類的 IsolatedStorageFileStream 對(duì)象來(lái)管理文件內(nèi)容,它的定義大概如下所示:
在Silverlight 2中支持兩種方式的獨(dú)立存儲(chǔ),即按應(yīng)用程序存儲(chǔ)或者按站點(diǎn)存儲(chǔ),可以分別使用GetUserStoreForApplication方法和GetUserStoreForSite方法來(lái)獲取IsolatedStorageFile對(duì)象。下面看一個(gè)簡(jiǎn)單的示例,最終的效果如下圖所示:
下面來(lái)看各個(gè)功能的實(shí)現(xiàn):
創(chuàng)建目錄,直接使用CreateDirectory方法就可以了,另外還可以使用DirectoryExistes方法來(lái)判斷目錄是否已經(jīng)存在:
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("創(chuàng)建目錄成功!"); } }}
創(chuàng)建文件,通過(guò)CreateFile方法來(lái)獲取一個(gè)IsolatedStorageFileStream,并將內(nèi)容寫入到文件中:
void btnCreateFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem == null && this.txtDirectoryName.Text == "") { HtmlPage.Window.Alert("請(qǐng)先選擇一個(gè)目錄或者輸入目錄名"); 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("請(qǐng)先選擇目錄和文件!"); 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; }}
增加配額
在本文一開始我就提到獨(dú)立存儲(chǔ)嚴(yán)格的限制了應(yīng)用程序可以存儲(chǔ)的數(shù)據(jù)的大小,但是我們可以通過(guò)IsolatedStorageFile類提供的IncreaseQuotaTo方法來(lái)申請(qǐng)更大的存儲(chǔ)空間,空間的大小是用字節(jié)作為單位來(lái)表示的,如下代碼片段所示,申請(qǐng)獨(dú)立存儲(chǔ)空間增加到5M:
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()){ long newQuetaSize = 5242880; long curAvail = store.AvailableFreeSpace; if (curAvail < newQuetaSize) { store.IncreaseQuotaTo(newQuetaSize); }}
當(dāng)我們?cè)噲D增加空間大小時(shí)瀏覽器將會(huì)彈出一個(gè)確認(rèn)對(duì)話框,供我們確認(rèn)是否允許增加獨(dú)立存儲(chǔ)的空間大小。
文件被存往何處
既然獨(dú)立獨(dú)立存儲(chǔ)是存放在客戶端本地,那到底存放在何處呢?在我個(gè)人計(jì)算機(jī)上的地址為:C:/Users/TerryLee/AppData/LocalLow/Microsoft/Silverlight/is/035kq51b.2q4/pksdhgue.3rx/1,不同機(jī)器會(huì)有一些變化,另外在XP下的存儲(chǔ)位置與Vista是不相同的。在g文件夾下面,我們找到當(dāng)前應(yīng)用程序的一些公有信息,可以看到有如下三個(gè)文件:
id.dat記錄了當(dāng)前應(yīng)用程序的ID
quota.dat記錄了當(dāng)前應(yīng)用程序獨(dú)立存儲(chǔ)的配額,即存儲(chǔ)空間大小
used.dat記錄已經(jīng)使用的空間
在另一個(gè)s文件夾下可以找到我們創(chuàng)建的目錄以及文件,并且可以打開文件來(lái)看到存儲(chǔ)的內(nèi)容,如下圖所示:
禁用獨(dú)立存儲(chǔ)
現(xiàn)在我們來(lái)思考一個(gè)問(wèn)題,既然獨(dú)立存儲(chǔ)是一個(gè)與Cookie機(jī)制類似的局部信任機(jī)制,我們是否也可以禁用獨(dú)立存儲(chǔ)呢?答案自然是肯定的。在Silverlight應(yīng)用程序上點(diǎn)擊右鍵時(shí),選擇Silverlight Configuration菜單,將會(huì)看到如下窗口:
在這里我們可以看到每一個(gè)應(yīng)用程序存儲(chǔ)空間的大小以及當(dāng)前使用的空間;可以刪除應(yīng)用程序獨(dú)立存儲(chǔ)數(shù)據(jù)或者禁用獨(dú)立存儲(chǔ)的功能。
獨(dú)立存儲(chǔ)配置
最后在簡(jiǎn)單說(shuō)一下獨(dú)立存儲(chǔ)配置,在Beta 1時(shí)代是應(yīng)用程序配置,現(xiàn)在不僅支持應(yīng)用程序配置,同時(shí)還支持站點(diǎn)配置,我們可以用它來(lái)存儲(chǔ)應(yīng)用程序配置如每個(gè)頁(yè)面顯示的圖片數(shù)量,頁(yè)面布局自定義配置等等,使用IsolatedStorageSettings類來(lái)實(shí)現(xiàn),該類在設(shè)計(jì)時(shí)使用了字典來(lái)存儲(chǔ)名-值對(duì),它的使用相當(dāng)簡(jiǎn)單:
IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;appSettings.Add("mykey","myValue");appSettings.Save();IsolatedStorageSettings siteSettings = IsolatedStorageSettings.SiteSettings;siteSettings.Add("mykey1","myValue1");siteSettings.Save();
獨(dú)立存儲(chǔ)配置的機(jī)制與我們上面講的一樣,它也是基于本地文件存儲(chǔ),系統(tǒng)默認(rèn)的會(huì)創(chuàng)建一個(gè)名為__LocalSettings的文件進(jìn)行存儲(chǔ),如下圖所示:
打開文件后可以看到,存儲(chǔ)的內(nèi)容(此處進(jìn)行了整理)
<ArrayOfKeyValueOfstringanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"> <KeyValueOfstringanyType> <Key>mykey</Key> <Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema" i:type="d3p1:string">myValue</Value> </KeyValueOfstringanyType></ArrayOfKeyValueOfstringanyType>
值得一提的是使用獨(dú)立存儲(chǔ)配置不僅僅可以存儲(chǔ)簡(jiǎn)單類型的數(shù)據(jù),也可以存儲(chǔ)我們自定義類型的數(shù)據(jù)。
小結(jié)
本文詳細(xì)介紹了Silverlight 2中的獨(dú)立存儲(chǔ)機(jī)制,希望對(duì)大家有所幫助。
NET技術(shù):詳解Silverlight 2中的獨(dú)立存儲(chǔ)(Isolated Storage),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。