Windows Phone 7之隔离存储空间

Windows Phone 7之隔离存储空间
2011年11月30日
  Windows Phone 7的隔离存储空间
  1. 概念:
  Windows Phone 7中所有的文件IO操作被限制在隔离存储空间里面,在隔离存储空间里面可以增加,删除和修改目录文件,在隔离存储空间里面可以存储程序的配置信息,但是每个应用程序的隔离存储空间都是独立的,相当于Windows Phone 的一块内存被单独划出来了,只有这一块的内部(应用程序本身)才可以访问其内部的信息,而外部(其他的应用程序)无法访问。
  2. 目录操作
  两个重要的类:
  IsolatedStorageFile:用于操作隔离存储空间里面的目录以及文件,例如增,删,改,查等。
  IsolatedStorageFileStream:用于读写操控隔离存储空间里面的文件流,例如当我们需要往某个文件写入东西的时候便会使用到这个类。
  IsolatedStorageSettings:用于存储程序的配置信息的Dictionary,例如应用程序的一些Key和Value等。
  3. 使用隔离存储空间需要引用两个命名空间:
  using[b] [/b]System.IO.IsolatedStorage[b];[/b]
  using[b] [/b]System.IO[b];[/b]
  对隔离存储空间的操作和传统的文件IO操作类似[b]。[/b]
  在隔离存储空间里里面没有绝对路径或则说没有根目录,通常来说例如在我们的Windows 电脑上,一个文件夹它的根目录在C盘或则D盘或则E盘等等,但是在Windows Phone 7中则没有根目录,因此也就没有绝对路径。所以我们要取得应用程序的隔离存储空间不能通过路径来获得,只能通过GetUserStoreForApplication[b]()[/b]方法来获得。
  [b]4. [/b][b]示例演示:[/b]
  [b] [/b]using[b] [/b]System.IO.IsolatedStorage[b];[/b]
  using[b] [/b]System.IO[b];[/b]
  [b] [/b]private[b] [/b]const[b] [/b]string[b] [/b]FolderName[b] [/b]=[b] [/b]"temp1"[b];[/b]//定义一个常量,必须在此初始化
  [b] [/b]private[b] [/b]void[b] [/b]newbutton_Click[b]([/b]object[b] [/b]sender[b],[/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  using[b] ([/b]IsolatedStorageFile[b] [/b]file[b] [/b]=[b] [/b]IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  //使用using表示这个类使用完之后可以自动的释放资源,即调用Dispose()方法。
  //获得应用程序的隔离存储空间
  [b] { [/b]
  [b] [/b]file.CreateDirectory[b]([/b]FolderName[b]);[/b]//新建一个文件夹[b][/b]
  [b] [/b]MessageBox.Show[b]([/b]"新建成功!"[b]);[/b]
  [b] }[/b]
  [b] }[/b]
  [b] [/b]
  [b] [/b]private[b] [/b]void[b] [/b]Checkbutton_Click[b]([/b]object[b] [/b]sender[b],[/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  [b] [/b]using[b] ([/b]IsolatedStorageFile[b][/b]file=IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  [b] [/b]if[b]([/b]file.DirectoryExists[b]([/b]FolderName[b]))[/b]//目录存在[b][/b]
  [b] {[/b]
  [b] [/b]MessageBox.Show[b]([/b]FolderName+"已经存在!"[b]);[/b]
  [b] }[/b]
  [b] [/b]else[b][/b]
  [b] {[/b]
  [b] [/b]MessageBox.Show[b]([/b]FolderName+"不存在!"[b]);[/b]
  [b] }[/b]
  [b] }[/b]
  [b] }[/b]
  [b] [/b]
  [b] [/b]private[b] [/b]void[b] [/b]Deletebutton_Click[b]([/b]object[b] [/b]sender[b],[/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  [b] [/b]using[b] ([/b]IsolatedStorageFile[b][/b]file=IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  [b] [/b]if[b]([/b]file.DirectoryExists[b]([/b]FolderName[b]))[/b]
  [b] {[/b]
  [b] [/b]file.DeleteDirectory[b]([/b]FolderName[b]);[/b]//删除目录
  [b] [/b]MessageBox.Show[b]([/b]FolderName[b][/b]+[b] [/b]"文件已删除"[b]);[/b]
  [b] }[/b]
  [b] [/b]else[b][/b]
  [b] {[/b]
  [b] [/b]MessageBox.Show[b]([/b]"没有可删除的文件"[b]);[/b]
  [b] }[/b]
  [b] }[/b]
  [b] }[/b]
  [b] [/b]
  5. 文件操作
  文件操作和目录的操作类似,也包括增,删,改,查等操作。
  a. 新建文件操作。
  using[b] ([/b]IsolatedStorageFile[b] [/b]file[b] [/b]=[b] [/b]IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  [b] [/b]FileStream[b] [/b]filestream[b] [/b]=[b] [/b]file.CreateFile[b]([/b]FileName[b]);[/b]//新建一个文件
  filestream.Close[b]();[/b]//关闭文件流,若不关闭,则删除操作出现异常
  //这里由于CreateFile()返回的是一个文件流,即FileStream,因此我们声明一个FileStream
  //变量来保存这个文件
  [b] }[/b]
  b. 删除文价操作
  using[b] ([/b]IsolatedStorageFile[b] [/b]file[b] [/b]=[b] [/b]IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  [b] [/b]if[b] ([/b]file.FileExists[b]([/b]FileName[b]))[/b]//检查文件
  [b] {[/b]
  [b] [/b]file.DeleteFile[b]([/b]FileName[b]);[/b]
  [b] }[/b]
  [b] }[/b]
  6. 文件信息的写入和读取(请看下面代码)
  [b] [/b]private[b] [/b]void[b] [/b]Writebutton_Click[b]([/b]object[b] [/b]sender[b],[/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  //获得应用程序的隔离存储空间
  using[b] ([/b]IsolatedStorageFile[b][/b]file[b] [/b]=[b] [/b]IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  //打开文件流,FileMode.OpenOrCreate表示如果文件不存在则新建文件,
  //FileAccess.Write表示是写入操作[b][/b]
  [b] [/b]using[b] ([/b]IsolatedStorageFileStream[b] [/b]filestream[b] [/b]=[b] [/b]file.OpenFile[b]([/b]FileName[b],[/b]FileMode.OpenOrCreate[b], [/b]FileAccess.Write[b]))[/b]
  [b] {[/b]
  [b] [/b]StreamWriter[b] [/b]strw[b] [/b]=[b] [/b]new[b] [/b]StreamWriter[b]([/b]filestream[b]);[/b]//指定往我们打开的文件流中写入信息[b][/b]
  [b] [/b]strw.WriteLine[b]([/b]MsgtextBox.Text[b]);[/b]//将MsgtextBox中的Text值写入文件中[b][/b]
  [b] [/b]strw.Close[b]();[/b]//关闭文件写入[b][/b]
  [b] }[/b]
  [b] }[/b]
  [b] }[/b]
  [b] [/b]
  [b] [/b]private[b] [/b]void[b] [/b]Readbutton_Click[b]([/b]object[b] [/b]sender[b],[/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  //获得应用程序的隔离存储空间[b][/b]
  using[b] ([/b]IsolatedStorageFile[b][/b]file[b] [/b]=[b] [/b]IsolatedStorageFile.GetUserStoreForApplication[b]())[/b]
  [b] {[/b]
  [b] [/b]if[b] ([/b]file.FileExists[b]([/b]FileName[b]))[/b]//如果文件存在才可进行读取[b][/b]
  [b] {[/b]
  //打开要读取的文件流,FileMode.Open表示只打开文件,
  //FileAccess.Read表示是进行读取操作[b][/b]
  [b] [/b]using[b] ([/b]IsolatedStorageFileStream[b] [/b]filestream[b] [/b]=[b] [/b]file.OpenFile[b]([/b]FileName[b],[/b]FileMode.Open[b], [/b]FileAccess.Read[b]))[/b]
  [b] {[/b]
  [b] [/b]StreamReader[b] [/b]strr[b] [/b]=[b] [/b]new[b] [/b]StreamReader[b]([/b]filestream[b]);[/b]//指定向打开的文件流读取信息[b][/b]
  [b] [/b]MsgtextBox.Text[b] [/b]=[b] [/b]strr.ReadToEnd[b]();[/b]//读取文件的全部信息[b][/b]
  [b] [/b]strr.Close[b]();[/b]//关闭读取操作[b][/b]
  [b] }[/b]
  [b] }[/b]
  [b] }[/b]
  [b] }[/b]
  首先我们需要获得应用程序的隔离存储空间,然后我们需要打开指定的文件(打开之后是以文件流的形式返回),在打开文件的过程中有几种情况并通过FileMode来确定我们要以那种方式打开。接着便可以对其文件进行相应的操作,通过FileAccess来确定我们是想进行读取还是写入的操作。
  7. 隔离存储空间的配置信息
  a. 往隔离存储空间的配置信息中写入信息
  [b] [/b]private[b] [/b]void[b] [/b]SettingsWritebutton_Click[b]([/b]object[b][/b]sender[b], [/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  [b] [/b]//将Settings放入ApplicationSettings的字典中,并将SettingstextBox的值赋给Settings[b][/b]
  [b] [/b]IsolatedStorageSettings.ApplicationSettings[b][[/b]Settings[b]][/b]=[b] [/b]SettingstextBox.Text[b];[/b]
  [b] [/b]IsolatedStorageSettings.ApplicationSettings.Save[b]();[/b]//保存信息,这一步非常关键,如果不保存,那么我们刚才添加的Settings信息就不会保存到应用程序的隔离存储空间中,而是放在了内存中,因此将无法读取Settings。[b][/b]
  [b] }[/b]
  b. 从隔离存储空间的配置信息中读取信息
  [b] [/b]private[b] [/b]void[b] [/b]SettingsReadbutton_Click[b]([/b]object[b][/b]sender[b], [/b]RoutedEventArgs[b] [/b]e[b])[/b]
  [b] {[/b]
  [b] [/b]//判断字典中是否有Settings这个Key,如果有则将其值赋给SettingstextBox[b][/b]
  [b] [/b]if[b] ([/b]IsolatedStorageSettings.ApplicationSettings.Contains[b]([/b]Settings[b]))[/b]
  [b] {[/b]
  [b] [/b]//由于字典中存放的是对象,取出来之后需要转换为相应的数据类型。[b][/b]
  [b] [/b]SettingstextBox.Text[b][/b]=[b] [/b]IsolatedStorageSettings.ApplicationSettings[b][[/b]Settings[b]][/b]as[b] [/b]string[b];[/b]
  [b] }[/b]
  [b] }[/b]
  以上便是隔离存储空间的相关内容,不正确的地方还望指出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值