<a href="http://webabcd.blog.51cto.com/1787395/342790" target="_blank">[索引頁]</a>
<a href="http://down.51cto.com/data/100302" target="_blank">[源碼下載下傳]</a>
穩紮穩打Silverlight(16) - 2.0資料之獨立存儲(Isolated Storage)
介紹
Silverlight 2.0 資料的獨立存儲(Isolated Storage):
IsolatedStorageFile - 操作 獨立存儲 的類
IsolatedStorageFile.GetUserStoreForSite() - 按站點擷取使用者的獨立存儲
IsolatedStorageFile.GetUserStoreForApplication() - 按應用程式擷取使用者的獨立存儲
IsolatedStorageSettings - 在獨立存儲中儲存的 key-value 字典表
IsolatedStorageSettings.SiteSettings - 按站點儲存的 key-value 字典表
IsolatedStorageSettings.ApplicationSettings - 按應用程式儲存的 key-value 字典表
線上DEMO
示例
IsolatedStorage.xaml
<UserControl x:Class="Silverlight20.Data.IsolatedStorage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel HorizontalAlignment="Left">
<TextBox x:Name="txtMsg" Margin="5" />
<TextBox x:Name="txtMsg2" Margin="5" />
<Button x:Name="increase" Content="增加配額" Click="increase_Click" Margin="5" />
</StackPanel>
</UserControl>
IsolatedStorage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
namespace Silverlight20.Data
{
public partial class IsolatedStorage : UserControl
{
public IsolatedStorage()
{
InitializeComponent();
// 示範 IsolatedStorageFile
Demo();
// 示範 IsolatedStorageSettings
Demo2();
}
/// <summary>
/// 示範 IsolatedStorageFile
/// </summary>
void Demo()
// Isolated Storage - 獨立存儲。一個虛拟檔案系統
// IsolatedStorageFile - 操作 獨立存儲 的類
// IsolatedStorageFile.GetUserStoreForSite() - 按站點擷取使用者的獨立存儲
// IsolatedStorageFile.GetUserStoreForApplication() - 按應用程式擷取使用者的獨立存儲
// using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
// DirectoryExists(path) - 指定的路徑是否存在
// CreateDirectory(path) - 建立指定的路徑
// FileExists(path) - 指定的檔案是否存在
// CreateFile(path) - 建立指定的檔案
// GetDirectoryNames() - 擷取根目錄下的目錄名數組
// GetFileNames()() - 擷取根目錄下的檔案名數組
// GetDirectoryNames(path) - 擷取指定目錄下的目錄名數組
// GetFileNames(path) - 擷取指定目錄下的檔案名數組
// OpenFile() - 打開指定的檔案。具體參數參看文檔
// DeleteFile(path) - 删除指定的檔案
// DeleteDirectory(path) - 删除指定的目錄(要求目錄存在,且目錄内無内容)
// Remove() - 關閉 IsolatedStorageFile 對象并移除獨立存儲内的全部内容
// 在根目錄下建立指定的目錄
if (!isf.DirectoryExists("Directory01"))
isf.CreateDirectory("Directory01");
if (!isf.DirectoryExists("Directory02"))
isf.CreateDirectory("Directory02");
// 建立指定的子目錄
string subDirectory01 = System.IO.Path.Combine("Directory01", "SubDirectory01");
string subDirectory02 = System.IO.Path.Combine("Directory01", "SubDirectory02");
if (!isf.DirectoryExists(subDirectory01))
isf.CreateDirectory(subDirectory01);
if (!isf.DirectoryExists(subDirectory02))
isf.CreateDirectory(subDirectory02);
// 根目錄下建立指定的檔案
if (!isf.FileExists("RootFile.txt"))
{
IsolatedStorageFileStream isfs = isf.CreateFile("RootFile01.txt");
isfs.Close();
}
// 在指定的目錄下建立指定的檔案
string file01 = System.IO.Path.Combine(subDirectory01, "File01.txt");
string file02 = System.IO.Path.Combine(subDirectory01, "File02.txt");
string file03 = System.IO.Path.Combine(subDirectory01, "File03.xml");
if (!isf.FileExists(file01))
// IsolatedStorageFileStream - 獨立存儲内的檔案流。繼承自 FileStream
IsolatedStorageFileStream isfs = isf.CreateFile(file01);
if (!isf.FileExists(file02))
IsolatedStorageFileStream isfs = isf.CreateFile(file02);
if (!isf.FileExists(file03))
IsolatedStorageFileStream isfs = isf.CreateFile(file03);
txtMsg.Text += "根目錄下的目錄清單:\r\n";
// 擷取根目錄下的目錄名數組
foreach (string directoryName in isf.GetDirectoryNames())
txtMsg.Text += directoryName + "\r\n";
txtMsg.Text += "根目錄下的檔案清單:\r\n";
// 擷取根目錄下的檔案名數組
foreach (string fileName in isf.GetFileNames())
txtMsg.Text += fileName + "\r\n";
txtMsg.Text += "目錄 Directory01 下的目錄清單:\r\n";
// 擷取 Directory01 目錄下的目錄名數組
foreach (string directoryName in isf.GetDirectoryNames(subDirectory01))
txtMsg.Text += "目錄 Directory01/SubDirectory01 下的*.txt檔案清單:\r\n";
// 擷取 Directory01/SubDirectory01 目錄下的字尾名為 txt 的檔案名數組
foreach (string fileName in isf.GetFileNames(System.IO.Path.Combine(subDirectory01, "*.txt")))
if (isf.FileExists(file01))
// 在檔案 file01 中寫入内容
IsolatedStorageFileStream streamWrite = isf.OpenFile(file01, FileMode.Open, FileAccess.Write);
using (StreamWriter sw = new StreamWriter(streamWrite))
{
sw.WriteLine("我是:webabcd");
sw.WriteLine("我專注于asp.net, Silverlight");
}
txtMsg.Text += "檔案 File01.txt 的内容:\r\n";
// 讀取檔案 file01 中的内容
IsolatedStorageFileStream streamRead = isf.OpenFile(file01, FileMode.Open, FileAccess.Read);
using (StreamReader sr = new StreamReader(streamRead))
txtMsg.Text += sr.ReadToEnd();
// 删除檔案 file01
isf.DeleteFile(file01);
try
// 删除目錄 subDirectory01
isf.DeleteDirectory(subDirectory01);
catch (IsolatedStorageException ex)
// IsolatedStorageException - 操作臨時存儲失敗時抛出的異常
// 因為 subDirectory01 目錄内還有檔案,是以會抛異常
txtMsg.Text += ex.Message;
}
/// 示範 IsolatedStorageSettings
void Demo2()
// IsolatedStorageSettings - 在獨立存儲中儲存的 key-value 字典表
// IsolatedStorageSettings.SiteSettings - 按站點儲存的 key-value 字典表
// IsolatedStorageSettings.ApplicationSettings - 按應用程式儲存的 key-value 字典表
IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
// Add(key, value) - 添加一對 key-value
iss.Add("key", "value");
txtMsg2.Text += (string)iss["key"] + "\r\n";
// 修改指定的 key 的 value
iss["key"] = "value2";
// Remove(key) - 移除指定的 key
// Count - 字典表内的總的 key-value 數
iss.Remove("key");
txtMsg2.Text += iss.Count;
private void increase_Click(object sender, RoutedEventArgs e)
// 示範獨立存儲的配額的相關操作
// Quota - 目前配額(KB)
// IncreaseQuotaTo(newQuotaSize) - 增加到指定的配額
// AvailableFreeSpace - 目前的可用配額
isf.IncreaseQuotaTo(isf.Quota + 1 * 1024 * 1024);
System.Windows.Browser.HtmlPage.Window.Alert(
string.Format("目前配額:{0};可用配額:{1}", isf.Quota, isf.AvailableFreeSpace));
}
}
}
示範 IsolatedStorageFile 的運作結果:
根目錄下的目錄清單:
Directory01
Directory02
根目錄下的檔案清單:
RootFile01.txt
__LocalSettings
目錄 Directory01 下的目錄清單:
SubDirectory01
目錄 Directory01/SubDirectory01 下的*.txt檔案清單:
File01.txt
File02.txt
檔案 File01.txt 的内容:
我是:webabcd
我專注于asp.net, Silverlight
無法删除,目錄不為空或不存在。
示範 IsolatedStorageSettings 的運作結果:
value
value2
OK
本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/343094,如需轉載請自行聯系原作者