且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 独立存储Isolated Storage

更新时间:2022-10-03 10:20:36

Windows Phone 7 支持访问数据几种方式为: XML、Isolated Storage[独立存储]、Cloud[云存储],Windows Phone 7 上没有本地

数据库API可以利用 。

Isolated Storage[独立存储]有两种方式在本地存储你的数据。第一是通过库中的键/值对,叫做IsolatedStorageSettings。第二是通过创建真实的文件和目录,叫做IsolatedStorageFile。

(1)IsolatedStorageSettings
IsolatedStorageSettings允许你在一个字典中存储键/值对(注意,无需任何设定),然后再读取出来。这些数据会一直保存着,无论应用程序停止/启动,或者关机等等。除非你删除它,或者用户卸载你的应用程序,否则它一直存在。要记住的一点是在它被添加到字典中之前你无法读取它。

IsolatedStorageSettings 可提供一种将用户特定数据存储为本地 IsolatedStorageFile 中的键/值对的便捷方法。一种典型的用途是保存设置,例如,每页显示的图像数、页面布局选项等。

用户设置可以是特定于某个应用程序的,也可以是在同一个域中的多个应用程序之间共享的。ApplicationSettings 存储为每个应用程序的设置、每台计算机的设置以及每个用户的设置。其范围由应用程序 .xap 文件的完整路径来决定。SiteSettings 存储为每个域的设置、每台计算机的设置以及每个用户的设置。其范围由承载应用程序 .xap 文件的子域来决定。例如,位于 http://www.contoso.com/site1/application.xap 的应用程序将与位于 http://www.contoso.com/site2/application.xap 的应用程序具有不同的 ApplicationSettings。但是,这两个应用程序将共享相同的 SiteSettings,因为它们承载于同一个子域中。


using System.IO.IsolatedStorage;


IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//使用 ApplicationSettings 属性可创建用于在独立存储中存储键/值对的字典的新实例。
//ApplicationSettings 特定于某个用户和某个应用程序。应用程序范围由应用程序的完整路径决定。


private void InitializeSettings()
{
if (settings.Contains("emailFlag"))
{
EmailFlag.IsChecked = (bool)settings["emailFlag"];
}
else settings.Add("emailFlag", false);
}

private void EmailFlag_Unchecked(object sender, RoutedEventArgs e)
{
settings["emailFlag"] = false;
}

private void EmailFlag_Checked(object sender, RoutedEventArgs e)
{
settings["emailFlag"] = true;
}
}

(2)IsolatedStorageFile

IsolatedStorageFile表示包含文件和目录的独立存储区。使用IsolatedStorageFile是一种让你可以在用户的设备中存储真实文件的机制。

该类使独立存储的虚拟文件系统抽象化。IsolatedStorageFile 对象对应于特定的独立存储范围,在该范围中存在由 IsolatedStorageFileStream 对象表示的文件。应用程序可以使用独立存储将数据保存在文件系统中这些数据自己的独立部分,而不必在文件系统中指定特定的路径。

虚拟文件系统的根位于物理文件系统上经过模糊处理的每用户文件夹中。由主机提供的每个唯一标识符都映射为不同的根,该根为每个应用程序提供它自己的虚拟文件系统。应用程序不能从它自己的文件系统导航到另一个文件系统中。

因为独立存储区在特定程序集的范围内,所以其他大多数托管代码都不能访问您的代码的数据(高度受信任的托管代码和管理工具可以从其他程序集访问存储区)。非托管代码可以访问任何独立存储区。


例子:
在一个子目录中创建了一个文本文件,并读取文件中的内容。我们还可以创建和删除目录,子目录及文件。创建一个新的IsolatedStorageFile对象,并使用一个IsolatedStorageFileStream对象将它写入到驱动器中。

using System.IO.IsolatedStorage;
using System.IO;

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
//为程序获取一个虚拟的本地存储
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();

//创建一个新的文件夹
fileStorage.CreateDirectory("textFiles");

//创建一个txt文件的流
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.OpenOrCreate, fileStorage));
//向文件中写出内容
fileWriter.WriteLine(writeText.Text);
//关闭StreamWriter.
fileWriter.Close();
}

private void GetButton_Click(object sender, RoutedEventArgs e)
{
//为程序获取一个虚拟的本地存储
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//创建一个新的StreamReader
StreamReader fileReader = null;

try
{
//读取文件
fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open, fileStorage));
//读取内容
string textFile = fileReader.ReadLine();


viewText.Text = textFile;
fileReader.Close();
}
catch
{
viewText.Text = "Need to create directory and the file first.";
}
}

 

本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1079210