这是第四篇关于系列短文章“WP7 Isolated Storage系列”,专注于真实实用,并且有源代码的例子,而不是存粹理论。接下来我将要讨论关于如何从Isolated Storage中读取数据和保存数据到Isolated Storage。
· WP7 Isolated Storage 系列 - 1.Isolated Storage简介
· WP7 Isolated Storage 系列 - 2.创建文件夹和文件
· WP7 Isolated Storage 系列 - 3.使用IsolatedStorageSettings存储数据
· WP7 Isolated Storage 系列 - 4.读取和存储文本文件
· WP7 Isolated Storage 系列 - 5.使用XmlSerializer读取和存储XML文件
· WP7 Isolated Storage 系列 - 6.使用XmlWriter读取和存储XML文件
· WP7 Isolated Storage 系列 - 7.读取和存储图像
· WP7 Isolated Storage 系列 - 8.读取和存储拍摄的图像
· WP7 Isolated Storage 系列 - 9.读取和存储二进制文件
· WP7 Isolated Storage 系列 - 10.文件操作
· WP7 Isolated Storage 系列 - 11.建议和最佳实践
· WP7 Isolated Storage 系列 - 12.开源数据库和帮助库文件
让我们从创建一个简单的Windows Phone 7工程开始。下一步需要添加下面的namespaces到MainPage.xaml.cs中去(或者你可以在另一个页面中使用这个代码):
using System.IO;
using System.IO.IsolatedStorage;
读取和保存文件到Isolated Storage中在许多WP7程序中是非常普通的任务。
基本上我们使用IsolatedStorageStream类去读取,写入和创建文件到Isolated Storage中。因为这个类继承FileStream类,所以多数情况下,FileStream可以使用的地方你都可以使用IsolatedStorageStream的实例,比如构造一个StreamReader或者是StreamWriter。
保存新文本文件到Isolated Storage
在下面这个例子里首先我们将在Isolated Storage中创建一个新的名为myfile.txt的文本文件,然后写一些字符串内容到这个文本文件中去。
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//create new file
using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("myFile.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage)))
{
string someTextData = "This is some text data to be saved in a new text file in the IsolatedStorage!";
writeFile.WriteLine(someTextData);
writeFile.Close();
}
注意:创建文件时我们用FileMode.Create,当我们想写入文件时使用FileMode.Write!你可以在这里找到所有的关于FileAccess的列表。
写入内容到一个存在于Isolated Storage中的文本文件
在这个例子里我们将从Isolated Storage中打来一个存在的名为myfile.txt文本文件,然后写入一些额外的字符串内容进去。
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Open existing file
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Write);
using (StreamWriter writer = new StreamWriter(fileStream))
{
string someTextData = "Some More TEXT Added: !";
writer.Write(someTextData);
writer.Close();
}
注意:打开已存在的文件时我们用FileMode.Open,当我们想写入文件时使用FileMode.Write!你可以在这里找到所有的关于FileAccess的列表。
从Isolate Storage中读取文本文件
在这个例子里我们将从Isolated Storage中打开一个已存在的名为myfile.txt的文本文件然后去读它的内容。然后将其显示到一个TextBlock中。
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
this.text.Text = reader.ReadLine();
}
注意:打开已存在的文件时我们用FileMode.Open,当我们想读取文件时使用FileMode.Read!你可以在这里找到所有的关于FileAccess的列表。
从Isolated Storage的一个文件夹中读取/写入文本文件
· 写入
//Obtain the virtual store for application
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new folder and call it "ImageFolder"
myIsolatedStorage.CreateDirectory("TextFilesFolder");
//Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
//Also take the text contents from the txtWrite control and write it to myFile.txt
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("TextFilesFolder\\myNewFile.txt", FileMode.OpenOrCreate, myIsolatedStorage));
string someTextData = "This is some text data to be saved in a new text file in the IsolatedStorage!";
writeFile.WriteLine(someTextData);
writeFile.Close();
· 读取
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("TextFilesFolder\\myNewFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fileStream))
{
this.text1.Text = reader.ReadLine();
}
注意:关于对文件夹操作的内容请查看WP7 Isolated Storage 系列 - 2.创建文件夹和文件
最佳实践
1. 当对文件进行操作时总是使用using声明,因为它提供了一个方便的语法来确保正确的使用IDisposable对象。
2. 总是检查在你需要创建/读取的文件所在文件夹是否存在。
3. 在读取一个文件之前请检查是否存在。
在这篇文章里我谈到了读取和写入文本文件到Isolated Storage中。这里是全部的源代码。敬请期待接下来的文章。我希望这能够对你有帮助。
原文链接:http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage-read-and-save-text-files