WP7的独立存储--kk小结
WP7的独立存储--kk小结
WP7--独立存储:
1> 什么是独立存储?
任何安装在Windows Phone 7中的程序都可以访问自身的永久磁盘存储区域,该区域称为独立存储。此种存储方式将提供一个磁盘存储空间,他是一种虚拟的文件系统,能存储小量的数据;在默认的情况下,它只能存储1MB的文件;用户可能根据自己的要求可以对空间的大小进行合理的分配。其数据本身还是存储在本地文件系统中的,但是其实际对于应用程序是透明的,应用程序只能够访问当前用户在当前应用程序域的文件及文件夹。
如下图所示,每个应用程序将在独立存储中被分配一个独立的存储空间,称为应用程序数据存储文件夹,即该应用的独立存储根目录。应用程序可以调用独立存储API在该目录下存储数据,根据使用方式及功能的不同,独立存储空间又包含两部分(独立设置存储和独立文件存储)。
<—— 应用程序独立存储空间逻辑结构 (此图源自MSDN)
2> 设置和文件
WP7没有本地数据库API可以利用,提供的有XML、独立存储和云存储。Isolated Storage(独立存储)有两种方式在本地存储你的数据,第一是通过库中的键/值对,叫做IsolatedStorageSettings(独立设置存储),第二是通过创建真实的文件和目录,叫做IsolatedStorageFile(独立文件存储)。
2.1> IsolatedStorageSettings(独立设置存储)
IsolatedStorageSettings是支持保存应用程序存储区或网站存储中的键/值对的字典集合,它自动负责序列化对象,并将其保存在独立存储中。(已序列化的对象将保存到名为_LocalSettings的独立存储中的文件中。)它以键/值对方式提供一种快速数据访问方式,主要用于存储一些应用设置信息。对外表现为一个键值对集合,可以使用兼职对集合的语法来进行操作。
命名空间为:System.IO.IsolatedStorage;主要涉及System.IO.IsolatedStorage.IsolatedStorageSettings类。
2.1.1>
//创建操作独立设置存储必须的IsolatedStorageSettings类的对象
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//添加一个键值对
settings.Add(key,value);
或 根据key来获取相对应的value
settings[key]=value;
if(settings.Contains(key))
{
string value = settings[key].ToString();
}
//读取键值对
string kk = (string)settings["kk"];
//修改键值对
settings["kk"] = value;
//判断该键是否存在
string isExist = settings.Contains("kk");
//移除
settings.Remove("kk");
//清除
settings.Clear();
//保存
settings.Save();
2.1.2> 以下用一个例子来说明:(源自WP7完美开发征程) WP7主界面如下图 App1 所示:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13
14 using System.IO.IsolatedStorage;
15 namespace IsolatedStorageSettings
16 {
17 public partial class MainPage : PhoneApplicationPage
18 {
19 //创建操作独立设置存储必须的IsolatedStorageSettings类的对象
20 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
21 // 构造函数
22 public MainPage()
23 {
24 InitializeComponent();
25 }
26 //为4个按钮添加ChangeSettins事件处理程序
27 private void ChangeSettings(object sender, RoutedEvent e)
28 {
29 string key = txbKey.Text.Trim();
30 string value = txbValue.Text;
31
32 //表示按钮控件
33 Button ClickedButton = sender as Button;
34
35 switch (ClickedButton.Name)
36 {
37 //添加
38 case "btn_Add": settings.Add(key, value); break;
39 //修改
40 case "btn_Update": settings[key] = value; break;
41 //移除指定键值对
42 case "btn_Remove": settings.Remove(key); break;
43 //清除
44 case "btn_Clear": settings.Clear(); break;
45 }
46
47 //保存
48 settings.Save();
49
50 ReadAllSettings();
51 }
52 //显示所有的键值对
53 protected void ReadAllSettings()
54 {
55 string message = "All Setting Key-Values:"+System.Environment.NewLine;
56 foreach(string key in settings.Keys)
57 {
58 message += string.Format("{0}={1}\n",Key,settings[key].ToString());
59 }
60 teblockSettings.Text = message;
61 }
62
63 }
<—— 图App1 < —— 图App2
2.2> IsolatedStorageFile(独立文件存储)
IsolatedStorageFile提供类似于文件系统一样的访问方式,应用程序可以以文件形式在其中存储任意数据(包括文本、图像、音视频,甚至代码文件等),如同操作常规文件系统一样创建、删除、访问文件及文件夹。
命名空间为:System.IO.IsolatedStorage;主要涉及System.IO.IsolatedStorage.IsolatedStorageFile类。实际上,IsolatedStorage.IsolatedStorageFile类是FileStream类的一个子类。
2.2.1> IsolatedStorageFile 常用的方法如下:
方法名 说明
CreateDirectory() 创建一个新的独立存储文件夹
DeleteDirectory() 删除独立存储文件夹
CreateFile() 创建文件
DeleteFile() 删除文件
GetFileNames() 得到文件名称集合
GetDirectoryName() 得到文件夹名称集合
OpenFile() 打开文件
Remove() 移出所有的文件和文件夹
2.2.2> 文件读写操作的基本过程如下:
(1)用获得的IsolatedStorageFile对象的OpenFile方法创建一个IsolatedstorageFileStream对象(或者用IsolatedstorageFileStream的构造方法构造)。
(2)针对IsolatedstorageFileStream对象构造StreamReader或StreamWriter对象进行文件读写。
(3)不要忘记关闭流对鞋对象以释放资源。
示例代码1:
View Code
1 //为程序获取一个虚拟的本地存储
2 IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();
3 //创建一个新的文件夹,并创建一个newText.txt文档
4 myStorage.CreateDirectory("textFiles");
5 IsolatedStorageFileStream isolatedFileStream =
6 new IsolatedStorageFileStream("textFiles//newText.txt", FileMode.OpenOrCreate, fileStorage);
7 //写入数据
8 StreamWriter fileWriter = new StreamWriter(isolatedFileStream);
9 fileWriter.WriteLine(newText.txt);
10 //关闭StreamWriter.
11 fileWriter.Close();
12 //读取数据
13 StreamReader fileReader=new StreamReader(isolatedFileStream);
14 String str=fileReader.ReadLine();
15 fileReader.Close();
16 //判断存储区某文件是否存在,参数是文件路径
17 fileStorage.FileExists("file path");
也可将以上的创建和读取代码放入using语句中,更改为下面的代码
示例代码2:
View Code
1 // 创建一个新的文件夹,并创建一个newText.txt文档
2 myStorage.CreateDirectory("textFiles");
3 using (var isolatedFileStream = new IsolatedStorageFileStream("textFiles//newText.txt", FileMode.OpenOrCreate, myStore))
4 {
5 //写入数据 www.2cto.com
6 using (var fileWriter = new StreamWriter(isolatedFileStream))
7 {
8 fileWriter.WriteLine(newText.txt);
9 }
10 }
11
12 //将文字读出来,放入try..catch语句中处理异常
13
14 IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();
15 try
16 {
17 // 指出具体路径
18 using (var isolatedFileStream = new IsolatedStorageFileStream(textFiles//myFile.txt", FileMode.Open, myStore))
19 {
20 // 读取数据
21 using (var fileReader = new StreamReader(isolatedFileStream))
22 {
23 txtRead.Text = fileReader.ReadLine();
24 }
25 }
26 }
27 catch
28 {
29 //当用户第一次点击Read按钮时处理异常
30 txtRead.Text = "需要第一次创建文件";
31 }
2.2.3> 以下用一个例子来展示独立存储中基本的文件读写操作。(源自WP7完美开发征程) WP7主界面如上图 App2 所示:
View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13
14 using System.IO.IsolatedStorage;
15 using System.IO;
16 namespace PhoneApp19
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 //为程序获取一个虚拟的本地存储
21 IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication();
22 // 构造函数
23 public MainPage()
24 {
25 InitializeComponent();
26 }
27 //写入数据
28 private void btnWrite_Click(object sender, RoutedEventArgs e)
29 {
30 string filePath = txbFilePath.Text.Trim();
31 string fileName = txbFileName.Text.Trim();
32 string fullFileName = System.IO.Path.Combine(filePath,fileName);
33 string content = txbContent.Text;
34 //判断文件是否存在,若不存在则创建
35 if (!storageFile.DirectoryExists(filePath))
36 {
37 storageFile.CreateDirectory(filePath);
38 }
39 //写入
40 using (StreamWriter writer = new StreamWriter(storageFile.OpenFile(fullFileName, FileMode.Append)))
41 {
42 writer.WriteLine(content);
43 }
44 }
45 //读取数据
46 private void btnRead_Click(object sender, RoutedEventArgs e)
47 {
48 string fullFilePath = txbFullFilePath.Text.Trim();
49
50 if (!storageFile.FileExists(fullFilePath))
51 {
52 txbReadContent.Text = "指定文件不存在";
53 return;
54 }
55 //读取
56 using (StreamReader reader = new StreamReader(storageFile.OpenFile(fullFilePath, FileMode.Open)))
57 {
58 txbReadContent.Text = reader.ReadToEnd();
59 }
60 }
61
62 }
63 }
kk--附:
创建命名空间时的一个快捷方式:
当清楚属性 IsolatedStorageSettings 时,写出属性 IsolatedStorageSettings ,并输入,按住Ctrl键同时输入.即打开一个小helper窗口,在窗口中的那个小using语句高亮显示时,单机键盘上的Return或Enter键,便在页面开头部分添加了一个新的using语句,即 using System.IO.IsolateStorage 。
kk--小感:
这是我第一次在博客园上发表文章,文章写的有点死板,不是很成熟,可能也有些知识点没有涉及到,希望读者能给我意见或建议吧。万事开头难,我会努力的,fighting!
摘自 kefira
相关文章
- 暂无相关文章
用户评论