詳解Silverlight 2中的獨立存儲(轉載)

獨立存儲(Isolated Storage)是Silverlight 2中提供的一個客戶端安全的存儲,它是一個與Cookie機制類似的局部信任機制。獨立存儲機制的APIs 提供了一個虛擬的文件系統和可以訪問這個虛擬文件系統的數據流對象。Silverlight中的獨立存儲是基於 .NET Framework中的獨立存儲來建立的,所以它僅僅是.NET Framework中獨立存儲的一個子集。

Silverlight中的獨立存儲有以下一些特徵:

1.每個基於Silverlight的應用程序都被分配了屬於它自己的一部分存儲空間, 但是應用程序中的程序集卻是在存儲空間中共享的。一個應用程序被服務器賦給了一個唯一的固定的標識值。基於Silverlight的應用程序的虛擬文件系統現在就以一個標識值的方式來訪問了。這個標識值必須是一個常量,這樣每次應用程序運行時纔可以找到這個共享的位置。

2.獨立存儲的APIs 其實和其它的文件操作APIs類似,比如 File 和 Directory 這些用來訪問和維護文件或文件夾的類。 它們都是基於FileStream APIs 來維護文件的內容的。

3.獨立存儲嚴格的限制了應用程序可以存儲的數據的大小,目前的上限是每個應用程序爲1 MB。

使用獨立存儲

Silverlight中的獨立存儲功能通過密封類IsolatedStorageFile來提供,位於命名空間System.IO.IsolatedStorag中,IsolatedStorageFile類抽象了獨立存儲的虛擬文件系統。創建一個 IsolatedStorageFile 類的實例,可以使用它對文件或文件夾進行列舉或管理。同樣還可以使用該類的 IsolatedStorageFileStream 對象來管理文件內容,它的定義大概如下所示:



在Silverlight 2中支持兩種方式的獨立存儲,即按應用程序存儲或者按站點存儲,可以分別使用GetUserStoreForApplication方法和GetUserStoreForSite方法來獲取IsolatedStorageFile對象。下面看一個簡單的示例,最終的效果如下圖所示:



下面來看各個功能的實現:

創建目錄,直接使用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:UsersTerryLeeAppDataLocalLowMicrosoftSilverlightis5kq51b.2q4pksdhgue.3rx,不同機器會有一些變化,另外在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>mykey</Key>
<Value xmlns:d3p1="http://www.w3.org/2001/XMLSchema"
i:type="d3p1:string">myValue</Value>
</KeyValueOfstringanyType>
</ArrayOfKeyValueOfstringanyType>

值得一提的是使用獨立存儲配置不僅僅可以存儲簡單類型的數據,也可以存儲我們自定義類型的數據。

小結

本文詳細介紹了Silverlight 2中的獨立存儲機制,希望對大家有所幫助。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章