using Sy
stem;
using Sy
stem.Collection
s.Generi
c;
using Sy
stem.Linq;
using Sy
stem.Net;
using Sy
stem.Windows;
using Sy
stem.Window
s.Controls;
using Sy
stem.Window
s.Documents;
using Sy
stem.Window
s.Input;
using Sy
stem.Window
s.Media;
using Sy
stem.Window
s.Media.Animation;
using Sy
stem.Window
s.Shapes;
using Sy
stem.IO.IsolatedStorage;
using Sy
stem.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 = Sy
stem.IO.Path.Combine(
"Directory01",
"subdirectory01");
String subdirectory02 = Sy
stem.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");
isf
s.Close(
);
}
// 在指定的目录下创建指定的文件
String file01 = Sy
stem.IO.Path.Combine(
subdirectory01,
"File01.txt");
String file02 = Sy
stem.IO.Path.Combine(
subdirectory01,
"File02.txt");
String file03 = Sy
stem.IO.Path.Combine(
subdirectory01,
"File03.xml");
if (!isf.FileExists(file01))
{
// IsolatedStorageFileStream - 独立存储内的文件流。继承自 FileStream
IsolatedStorageFileStream isfs = isf.CreateFile(file01
);
isf
s.Close(
);
}
if (!isf.FileExists(file02))
{
IsolatedStorageFileStream isfs = isf.CreateFile(file02
);
isf
s.Close(
);
}
if (!isf.FileExists(file03))
{
IsolatedStorageFileStream isfs = isf.CreateFile(file03
);
isf
s.Close(
);
}
tx
tmsg.Text +=
"根目录下的目录列表:\r\n";
// 获取根目录下的目录名数组
foreach (
String directoryName
in isf.GetDirectoryNames())
{
tx
tmsg.Text += directoryName +
"\r\n";
}
tx
tmsg.Text +=
"根目录下的文件列表:\r\n";
// 获取根目录下的文件名数组
foreach (
String fil
ename
in isf.GetFil
enames())
{
tx
tmsg.Text += fil
ename +
"\r\n";
}
tx
tmsg.Text +=
"目录 Directory01 下的目录列表:\r\n";
// 获取 Directory01 目录下的目录名数组
foreach (
String directoryName
in isf.GetDirectoryNames(
subdirectory01))
{
tx
tmsg.Text += directoryName +
"\r\n";
}
tx
tmsg.Text +=
"目录 Directory01/subdirectory01 下的*.txt文件列表:\r\n";
// 获取 Directory01/subdirectory01 目录下的后缀名为 txt 的文件名数组
foreach (
String fil
ename
in isf.GetFil
enames(Sy
stem.IO.Path.Combine(
subdirectory01,
"*.txt")))
{
tx
tmsg.Text += fil
ename +
"\r\n";
}
if (isf.FileExists(file01))
{
// 在文件 file01 中写入内容
IsolatedStorageFileStream streamWrite = isf
.openFile(file01,FileMode
.open,FileAcces
s.Writ
E);
using (StreamWriter sw =
new StreamWriter(streamWrit
E))
{
sw.WriteLine(
"我是:webabcd");
sw.WriteLine(
"我专注于asp.net,Silverlight");
}
tx
tmsg.Text +=
"文件 File01.txt 的内容:\r\n";
// 读取文件 file01 中的内容
IsolatedStorageFileStream streamRead = isf
.openFile(file01,FileAcces
s.Read
);
using (StreamReader sr =
new StreamReader(streamRead))
{
tx
tmsg.Text += sr.ReadToEnd(
);
}
}
// 删除文件 file01
if (isf.FileExists(file01))
{
isf.
deleteFile(file01
);
}
try
{
// 删除目录 subdirectory01
isf.
deleteDirectory(
subdirectory01
);
}
catch (IsolatedStorageException e
X)
{
// IsolatedStorageException - 操作临时存储失败时抛出的异常
// 因为 subdirectory01 目录内还有文件,所以会抛异常
tx
tmsg.Text += ex.
message;
}
}
}
/// <sumMary>
/// 演示 IsolatedStorageSetTings
/// </sumMary>
void Demo2()
{
// IsolatedStorageSetTings - 在独立存储中保存的 key-value 字典表
// IsolatedStorageSetTings.SiteSetTings - 按站点保存的 key-value 字典表
// IsolatedStorageSetTings.ApplicationSetTings - 按应用程序保存的 key-value 字典表
IsolatedStorageSet
Tings iss = IsolatedStorageSet
Ting
s.ApplicationSet
Tings;
// Add(key,value) - 添加一对 key-value
is
s.Add(
"key",
"value");
tx
tmsg2.Text += (
String)iss[
"key"] +
"\r\n";
// 修改指定的 key 的 value
iss[
"key"] =
"value2";
tx
tmsg2.Text += (
String)iss[
"key"] +
"\r\n";
// Remove(key) - 移除指定的 key
// Count - 字典表内的总的 key-value 数
is
s.Remove(
"key");
tx
tmsg2.Text += is
s.Count;
}
private
void increase_Click(
object sender,RoutedEventArgs
E)
{
// 演示独立存储的配额的相关操作
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
// Quota - 当前配额(KB)
// IncreaseQuotaTo(newQuotaSizE) - 增加到指定的配额
// AvailableFreeSpace - 当前的可用配额
isf.IncreaseQuotaTo(isf.Quota + 1 * 1024 * 10
24);
Sy
stem.Windows
.browser.HtmlPage.Window.Alert(
String.Format(
"当前配额:{0};可用配额:{1}",isf.Quota,isf.AvailableFreeSpac
E));
}
}
}
}