IsolatedStorage独立存储空间是保存应用程序的一些数据已经配置文件,独立存储空间相对于其他的wp程序是独立的,也就是说每个wp程序都会有自己的独立存储空间,每个wp程序相互之间不能访问;
什么是Isolated Storage?
Isolated Storage又叫做隔离存储空间,Windows Phone 7手机上用来本地存储数据。下图是一个存储应用的文件夹结构图:
Isolated Storage用来创建与维护本地存储。WP7上的架构和Windows下的Silverlight类似,所有的读写操作都只限于隔离存储空间并且无法直接访问磁层操作系统的文件系统。这样能够防止非法的访问以及其他应用的破坏,增强安全性。
提示:如果你有两个应用想要共用一个同一个数据,则没法通过本地存储实现。你可以使用web服务等。
提示:WP7下的隔离存储空间没有配额的限制。应用应该只保存必要的数据。当Windows Phone只剩下10%的可用空间,用户会收到一个提醒并可能停止当前应用。对用户来讲这是一个很差的用户体验。
在隔离存储空间下可以进行目录操作、文件操作、应用程序配置信息等。
什么是Isolated Storage部分参考出处:http://www.cnblogs.com/zdave/archive/2011/05/06/2038924.html
-
IsolatedStorageFile类
此类表示包含文件和目录的独立存储区
IsolatedStorageFileisStore=IsolatedStorageFile.GetUserStoreForApplication();
属性
表示独立存储的可用空间量单位为字节
该值表示独立存储的最大可用空间量不能超过该值单位为字节
创建目录
创建文件
判断是否存在某个目录,删除目录之前可调用该方法
删除创建的目录
判断是否存在某个文件,删除文件之前可调用该方法
删除传进的文件
得到匹配的目录名称这里string支持通配符:单字节(“?”)和多字节(“*”)
得到匹配的文件名称这里string支持通配符:单字节(“?”)和多字节(“*”)
获取应用程序级的独立存储空间
比较重要的方法,增加独立存储空间空间量,但不可超过Quota
移除独立存储区范围及其所有内容,利用此方法必须先判断文件和目录是否正在使用,如果正在使用会有异常
-
IsolatedStorageFileStream类
此类是文件流,实现对文件的操作
IsolatedStorageFileStreamisStream= newIsolatedStorageFileStream( " test\\TEST.txt ",System.IO.FileMode.Open,FileAccess.Read,isStore);
属性
是否可读默认为true
是否可检索默认为true
是否可写默认为true
文件流写入和读取的路径
设置的流读取超时时间
设置的写入流超时时间
在异步的时候用到,开始读取
在异步的时候用到,读取结束
在异步的时候用到,开始写入
在异步的时候用到,写入结束
关闭当前流并释放相关资源
从当前流读取所有字节并写入目标流
写入单个字节
读取单个字节
写入字节块bytes
读取文件获得字节块bytes
限制流的长度
-
此类是存储一些配置信息,实例化
varsettings=IsolatedStorageSettings.ApplicationSettings;// 添加内容
settings.Add( " key ", object);
// 保存
settings.Save();// 获取配置信息
stringvalue=settings[ " key "].ToString();
// out传参获取值
stringvalue;
settings.TryGetValue( " key ", outvalue); - 初始化界面
View Code
<phone:PhoneApplicationPage
x:Class= " IsolatedStorageFileStreamApplication.MainPage "
xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x= " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:phone= " clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone "
xmlns:shell= " clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone "
xmlns:d= " http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
mc:Ignorable= " d "d:DesignWidth= " 480 "d:DesignHeight= " 768 "
FontFamily= " {StaticResourcePhoneFontFamilyNormal} "
FontSize= " {StaticResourcePhoneFontSizeNormal} "
Foreground= " {StaticResourcePhoneForegroundBrush} "
SupportedOrientations= " Portrait "Orientation= " Portrait "
shell:SystemTray.IsVisible= " True ">
<!--LayoutRoot istherootgrid whereallpagecontent isplaced-->
<Gridx:Name= " LayoutRoot "Background= " Transparent ">
<Grid.RowDefinitions>
<RowDefinitionHeight= " Auto "/>
<RowDefinitionHeight= " * "/>
</Grid.RowDefinitions>
<!--TitlePanelcontainsthenameoftheapplicationandpagetitle-->
<StackPanelx:Name= " TitlePanel "Grid.Row= " 0 "Margin= " 12,17,0,28 ">
<TextBlockx:Name= " ApplicationTitle "Text= " MYAPPLICATION "Style= " {StaticResourcePhoneTextNormalStyle} "/>
<TextBlockx:Name= " PageTitle "Text= " pagename "Margin= " 9,-7,0,0 "Style= " {StaticResourcePhoneTextTitle1Style} "/>
</StackPanel>
<!--ContentPanel-placeadditionalcontenthere-->
<Gridx:Name= " ContentPanel "Grid.Row= " 1 "Margin= " 12,0,12,0 ">
<Buttonx:Name= " btnWrite "Margin= " 0,400,300,100 "Click= " btnWrite_Click "Content= " 写操作 "></Button>
<Buttonx:Name= " btnRead "Margin= " 150,400,150,100 "Content= " 读操作 "Click= " btnRead_Click "IsEnabled= " False "></Button>
<Buttonx:Name= " btnDel "Margin= " 300,400,10,100 "Content= " 删除操作 "Click= " btnDel_Click "></Button>
<TextBlockx:Name= " txtShow "Margin= " 0,100,0,200 "Text= " 显示读取的资料或者状态 "HorizontalAlignment= " Center "VerticalAlignment= " Center "></TextBlock>
</Grid>
</Grid>
</phone:PhoneApplicationPage> - 删除操作
View Code
/// <summary>
/// 删除文件及路径
/// </summary>
/// <paramname="sender"></param>
/// <paramname="e"></param>
private voidbtnDel_Click( objectsender,RoutedEventArgse)
{
IsolatedStorageFileisoStore=IsolatedStorageFile.GetUserStoreForApplication();
// 判断是否存在
if(isoStore.FileExists( " test\\TEST.txt "))
{
// 删除文件
isoStore.DeleteFile( " test\\TEST.txt ");
// 删除目录
isoStore.DeleteDirectory( " test ");
// 完全删除
isoStore.Remove();
// 释放资源
isoStore.Dispose();
// 配置信息
varsettings=IsolatedStorageSettings.ApplicationSettings;
// 清空配置信息
settings.Clear();
}
txtShow.Text= " 删除完成 ";
} - 写入操作
View Code
/// <summary>
/// 写操作
/// </summary>
/// <paramname="sender"></param>
/// <paramname="e"></param>
private voidbtnWrite_Click( objectsender,RoutedEventArgse)
{
IsolatedStorageFileisoStore=IsolatedStorageFile.GetUserStoreForApplication();
// 独立存储空间可用空间量
longavailableSpace=isoStore.AvailableFreeSpace;
// 独立存储空间最大可用空间量
longquota=isoStore.Quota;
// 独立存储空间的可用量扩充字节为单位
// booldl=isoStore.IncreaseQuotaTo(quota);
// 创建目录
isoStore.CreateDirectory( " test ");
IsolatedStorageFileStreamisoStream= newIsolatedStorageFileStream( " test\\TEST.txt ",System.IO.FileMode.Create,isoStore);
byteonlyOneByte= 101;
isoStream.WriteByte(onlyOneByte);
isoStream.Close();
isoStore.Dispose();
btnRead.IsEnabled= true;
txtShow.Text= " 写入完成 ";
// 存储配置信息
varsettings=IsolatedStorageSettings.ApplicationSettings;
// 添加配置信息
settings.Add( " ip ", " 192.168.1.1 ");
// 保存
settings.Save();
} - 读取操作
View Code
/// <summary>
/// 读操作
/// </summary>
/// <paramname="sender"></param>
/// <paramname="e"></param>
private voidbtnRead_Click( objectsender,RoutedEventArgse)
{
IsolatedStorageFileisStore=IsolatedStorageFile.GetUserStoreForApplication();
if(isStore.FileExists( " test\\TEST.txt "))
{
IsolatedStorageFileStreamisStream= newIsolatedStorageFileStream( " test\\TEST.txt ",System.IO.FileMode.Open,FileAccess.Read,isStore);
// 获取路径
string[]directoryName=isStore.GetDirectoryNames( " test ");
// 获取文件名搜索模式。单字符("?")和多字符("*")通配符都受支持
string[]fileName=isStore.GetFileNames( " test\\T*.txt ");
txtShow.Text= " 写入资料值为: "+isStream.ReadByte().ToString()+ " ;\n路径为: "+directoryName[ 0].ToString()+ " ;\n文件名称为: "+fileName[ 0];
isStream.Close();
isStore.Dispose();
txtShow.Text=txtShow.Text+ " \n读取完成 ";
// 存储配置信息
varsettings=IsolatedStorageSettings.ApplicationSettings;
// 判断key是否存在
if(settings.Contains( " ip "))
{
// 只支持key的查询
stringip=settings[ " ip "].ToString();
// out传参获取值
stringipStr;
settings.TryGetValue( " ip ", outipStr);
txtShow.Text=txtShow.Text+ " \n配置文件ip: "+ip+ " ;\nout传参ip: "+ipStr;
}
}
} - 读取效果