色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

詳解Silverlight 2中的獨立存儲(Isolated Storage)

概述

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

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

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

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

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

使用獨立存儲

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

TerryLee_0072

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

 TerryLee_0073

 

 

下面來看各個功能的實現(xiàn):

創(chuàng)建目錄,直接使用CreateDirectory方法就可以了,另外還可以使用DirectoryExistes方法來判斷目錄是否已經(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)建文件,通過CreateFile方法來獲取一個IsolatedStorageFileStream,并將內(nèi)容寫入到文件中:

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;    }}

增加配額

在本文一開始我就提到獨立存儲嚴格的限制了應用程序可以存儲的數(shù)據(jù)的大小,但是我們可以通過IsolatedStorageFile類提供的IncreaseQuotaTo方法來申請更大的存儲空間,空間的大小是用字節(jié)作為單位來表示的,如下代碼片段所示,申請獨立存儲空間增加到5M:

using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()){    long newQuetaSize = 5242880;    long curAvail = store.AvailableFreeSpace;    if (curAvail < newQuetaSize)    {        store.IncreaseQuotaTo(newQuetaSize);    }}

當我們試圖增加空間大小時瀏覽器將會彈出一個確認對話框,供我們確認是否允許增加獨立存儲的空間大小。

TerryLee_0076 

文件被存往何處

既然獨立獨立存儲是存放在客戶端本地,那到底存放在何處呢?在我個人計算機上的地址為:C:/Users/TerryLee/AppData/LocalLow/Microsoft/Silverlight/is/035kq51b.2q4/pksdhgue.3rx/1,不同機器會有一些變化,另外在XP下的存儲位置與Vista是不相同的。在g文件夾下面,我們找到當前應用程序的一些公有信息,可以看到有如下三個文件:

TerryLee_0074

id.dat記錄了當前應用程序的ID

quota.dat記錄了當前應用程序獨立存儲的配額,即存儲空間大小

used.dat記錄已經(jīng)使用的空間

在另一個s文件夾下可以找到我們創(chuàng)建的目錄以及文件,并且可以打開文件來看到存儲的內(nèi)容,如下圖所示:

TerryLee_0075

禁用獨立存儲

現(xiàn)在我們來思考一個問題,既然獨立存儲是一個與Cookie機制類似的局部信任機制,我們是否也可以禁用獨立存儲呢?答案自然是肯定的。在Silverlight應用程序上點擊右鍵時,選擇Silverlight Configuration菜單,將會看到如下窗口:

TerryLee_0077

在這里我們可以看到每一個應用程序存儲空間的大小以及當前使用的空間;可以刪除應用程序獨立存儲數(shù)據(jù)或者禁用獨立存儲的功能。

獨立存儲配置

最后在簡單說一下獨立存儲配置,在Beta 1時代是應用程序配置,現(xiàn)在不僅支持應用程序配置,同時還支持站點配置,我們可以用它來存儲應用程序配置如每個頁面顯示的圖片數(shù)量,頁面布局自定義配置等等,使用IsolatedStorageSettings類來實現(xiàn),該類在設計時使用了字典來存儲名-值對,它的使用相當簡單:

IsolatedStorageSettings appSettings =     IsolatedStorageSettings.ApplicationSettings;appSettings.Add("mykey","myValue");appSettings.Save();IsolatedStorageSettings siteSettings =    IsolatedStorageSettings.SiteSettings;siteSettings.Add("mykey1","myValue1");siteSettings.Save();

獨立存儲配置的機制與我們上面講的一樣,它也是基于本地文件存儲,系統(tǒng)默認的會創(chuàng)建一個名為__LocalSettings的文件進行存儲,如下圖所示:

TerryLee_0078

打開文件后可以看到,存儲的內(nèi)容(此處進行了整理)

<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>

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

小結(jié)

本文詳細介紹了Silverlight 2中的獨立存儲機制,希望對大家有所幫助。

NET技術(shù)詳解Silverlight 2中的獨立存儲(Isolated Storage),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 无棣县| 平陆县| 剑阁县| 宜章县| 白水县| 广昌县| 台安县| 即墨市| 满洲里市| 页游| 柳江县| 肃南| 自治县| 涞水县| 孝昌县| 达孜县| 临城县| 大城县| 松阳县| 乐平市| 师宗县| 济南市| 新干县| 岢岚县| 集安市| 什邡市| 武隆县| 东辽县| 封开县| 和林格尔县| 来凤县| 麟游县| 南汇区| 祁阳县| 富源县| 措勤县| 盐边县| 金华市| 晴隆县| 贵州省| 新邵县|