在windows phone7上, Isolated storage相当于一个程序的独立存储区,只能供本程序使用,而不允许别的程序访问。
从开发上来说,以前的windows mobile程序安装后,在手机的资源管理器里能看到程序安装目录下的所有文件,包括你自己的一些资源文件,资源图像等等,就像PC上一样,但是在phone7上,没有办法可以看到这些文件,因为这些文件在工程Build的时候以Content或Resource的格式包括到了一个后缀名为.XAP的文件里了,你可以用解压软件打开这个XAP可以看到它实际上也是一个压缩包而已,但是这个包里的文件在程序运行时只能被程序读,而不允许修改。例如我们可以用System.Windows.Application.GetResourceStream()来读取一个文件到一个Stream,
但是我们不能反过来更改这个文件。
那么如果我们需要在程序运行期间修改某个文件,比如往某个文本文件里增加一些文字或者删除一些内容,要怎么办呢?
解决办法是: 我们需要在程序第一次在手机上运行的时候把这些文件复制到Isolated storage里,以后程序每次使用的时都从storage里读或写,原来打包的XAP里文件就可以不用去管它了。
实际上这种使用方法还有另外一个好处,那就是当你的程序具有版本升级功能时,当新版本下载完成后,要先卸载旧版软件,在卸载时会删除程序所有文件,但不会影响到Isolated storage里的文件,因此新版本安装后,你的Isolated storage里还存有你原来的文件而不会被覆盖掉。
下面我用代码演示一些Isolated storage的常见用法:
1. 下面的示例演示了如何向Isolated storage里写一个文本文件,然后再读出来
代码
using
System.IO;
02
using
System.IO.IsolatedStorage;
03
using
System.Windows;
04
using
Microsoft.Phone.Controls;
05
06
namespace
WP7IsoStorageDemo
07
{
08
public
partial
class
MainPage : PhoneApplicationPage
09
{
10
//
Constructor
11
public
MainPage()
12
{
13
InitializeComponent();
14
}
15
16
private
void
btnSave_Click(
object
sender, RoutedEventArgs e)
17
{
18
string
fileName
=
"
MyTextfile.txt
"
;
19
20
using
(var isoStorage
=
IsolatedStorageFile.GetUserStoreForApplication())
21
{
22
//
we need to check to see if the file exists
23
if
(
!
isoStorage.FileExists(fileName))
24
{
25
//
file doesn't exist...time to create it.
26
isoStorage.CreateFile(fileName);
27
}
28
29
//
since we are appending to the file, we must use FileMode.Append
30
using
(var isoStream
=
new
IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage))
31
{
32
//
opens the file and writes to it.
33
using
(var fileStream
=
new
StreamWriter(isoStream))
34
{
35
fileStream.WriteLine(txtText.Text);
36
}
37
}
38
39
//
you cannot read from a stream that you opened in FileMode.Append. Therefore, we need
40
//
to close the IsolatedStorageFileStream then open it again in a different FileMode. Since we
41
//
we are simply reading the file, we use FileMode.Open
42
using
(var isoStream
=
new
IsolatedStorageFileStream(fileName, FileMode.Open, isoStorage))
43
{
44
//
opens the file and reads it.
45
using
(var fileStream
=
new
StreamReader(isoStream))
46
{
47
txtCurrentText.Text
=
fileStream.ReadToEnd();
48
}
49
}
50
}
51
}
52
}
53
}
2. 下面演示:如何从你的工程里复制文件到Isolated storage, 这里要复制的文件是二进制格式的,因此代码中使用的是BinaryWriter 和 BinaryReader。
由于是用二进制读写,因此任何文件都可以操作,例如你可以拷贝.jpg文件到Isolated storage里,只要在创建文件时时候合适的文件后缀名即可。
代码
string
PersonDBFile
=
"
Resources\\person.db
"
;
//
Resources目录是工程里的一个子目录
using
(var isoStorage
=
IsolatedStorageFile.GetUserStoreForApplication())
{
#region
If PersonDB file is not exists in IsolatedStorage, copy it from XAP resources
if
(
!
isoStorage.FileExists(PersonDBFile))
{
using
(var isoStream
=
new
IsolatedStorageFileStream(PersonDBFile, FileMode.Create, isoStorage))
{
using
(var writer
=
new
BinaryWriter(isoStream))
{
string
PersonDBFileXap
=
string
.Format(CultureInfo.InvariantCulture, PersonDBFile);
StreamResourceInfo sri
=
Application.GetResourceStream(
new
Uri(
"
Resources/
"
+
PersonDBFileXap, UriKind.Relative));
using
(BinaryReader reader
=
new
BinaryReader(sri.Stream))
{
long
len
=
sri.Stream.Length;
for
(
long
i
=
0
; i
<
len; i
++
)
writer.Write(reader.ReadByte());
}
}
}
}
}
3.下面演示如何从Isolated storage里删除文件和目录
代码
void
delete_Click(
object
sender, EventArgs e)
{
using
(var isoStorage
=
IsolatedStorageFile.GetUserStoreForApplication())
{
string
[] filesInSubDir
=
isoStorage.GetFileNames(RefPhotosDir
+
"
\\
"
+
"
*.*
"
);
//
列出指定目录下所有文件
if
(isoStorage.FileExists(PersonDBFile))
//
判断文件是否存在
{
isoStorage.DeleteFile(PersonDBFile);
//
删除文件
}
if
(isoStorage.FileExists(MetadataFile))
{
isoStorage.DeleteFile(MetadataFile);
}
for
(
int
i
=
0
; i
<
filesInSubDir.Length; i
++
)
{
isoStorage.DeleteFile(RefPhotosDir
+
"
\\
"
+
filesInSubDir[i]);
}
isoStorage.DeleteDirectory(RefPhotosDirName);
//
删除目录
}
}