您的位置:首页 > 其它

windows phone 独立存储空间的操作

2012-04-26 08:55 274 查看
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

此类表示包含文件和目录的独立存储区

IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();

此类的实例化是得到整个程序的独立存储空间

属性

AvailableFreeSpace
表示独立存储的可用空间量 单位为字节
Quota
该值表示独立存储的最大可用空间量 不能超过该值 单位为字节
方法

CreateDirectory
创建目录
CreateFile
创建文件
DirectoryExists
判断是否存在某个目录,删除目录之前可调用该方法
DeleteDirectory
删除创建的目录
FileExists
判断是否存在某个文件,删除文件之前可调用该方法
DeleteFile
删除传进的文件
GetDirectoryNames(String)
得到匹配的目录名称 这里string支持通配符:单字节(“?”)和多字节(“*”)
GetFileNames(String)
得到匹配的文件名称 这里string支持通配符:单字节(“?”)和多字节(“*”)
GetUserStoreForApplication
获取应用程序级的独立存储空间
IncreaseQuotaTo
比较重要的方法,增加独立存储空间空间量,但不可超过Quota
Remove
移除独立存储区范围及其所有内容,利用此方法必须先判断文件和目录是否正在使用,如果正在使用会有异常


IsolatedStorageFileStream

此类是文件流,实现对文件的操作

IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("test\\TEST.txt", System.IO.FileMode.Open, FileAccess.Read, isStore);

此实例化类是文件进行操作

属性

CanRead
是否可读 默认为true
CanSeek
是否可检索 默认为true
CanWrite
是否可写 默认为true
Name
文件流写入和读取的路径
ReadTimeout
设置的流读取超时时间
WriteTimeout
设置的写入流超时时间
方法

BeginRead
在异步的时候用到,开始读取
EndRead
在异步的时候用到,读取结束
BeginWrite
在异步的时候用到,开始写入
EndWrite
在异步的时候用到,写入结束
Close
关闭当前流并释放相关资源
CopyTo(Stream)
从当前流读取所有字节并写入目标流
WriteByte
写入单个字节
ReadByte
读取单个字节
Write
写入字节块bytes
Read
读取文件获得字节块bytes
SetLength
限制流的长度
IsolatedStorageSettings

此类是存储一些配置信息,实例化




var settings = IsolatedStorageSettings.ApplicationSettings;

//添加内容

settings.Add("key", object);

//保存

settings.Save();

//获取配置信息

string value = settings["key"].ToString();

//out传参获取值

string value;

settings.TryGetValue("key", out value);




初始化界面


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="{StaticResource PhoneFontFamilyNormal}"

FontSize="{StaticResource PhoneFontSizeNormal}"

Foreground="{StaticResource PhoneForegroundBrush}"

SupportedOrientations="Portrait" Orientation="Portrait"

shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->

<Grid x:Name="LayoutRoot" Background="Transparent">

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">

<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>

<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

</StackPanel>

<!--ContentPanel - place additional content here-->

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

<Button x:Name="btnWrite" Margin="0,400,300,100" Click="btnWrite_Click" Content="写操作"></Button>

<Button x:Name="btnRead" Margin="150,400,150,100" Content="读操作" Click="btnRead_Click" IsEnabled="False" ></Button>

<Button x:Name="btnDel" Margin="300,400,10,100" Content="删除操作" Click="btnDel_Click"></Button>

<TextBlock x:Name="txtShow" Margin="0,100,0,200" Text="显示读取的资料或者状态" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>

</Grid>

</Grid>

</phone:PhoneApplicationPage>






删除操作


View
Code




/// <summary>

/// 删除文件及路径

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnDel_Click(object sender, RoutedEventArgs e)

{

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

//判断是否存在

if (isoStore.FileExists("test\\TEST.txt"))

{

//删除文件

isoStore.DeleteFile("test\\TEST.txt");

//删除目录

isoStore.DeleteDirectory("test");

//完全删除

isoStore.Remove();

//释放资源

isoStore.Dispose();

//配置信息

var settings = IsolatedStorageSettings.ApplicationSettings;

//清空配置信息

settings.Clear();

}

txtShow.Text = "删除完成";

}




写入操作


View
Code




/// <summary>

/// 写操作

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnWrite_Click(object sender, RoutedEventArgs e)

{

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

//独立存储空间可用空间量

long availableSpace = isoStore.AvailableFreeSpace;

//独立存储空间最大可用空间量

long quota = isoStore.Quota;

//独立存储空间的可用量扩充 字节为单位

// bool dl = isoStore.IncreaseQuotaTo(quota);

//创建目录

isoStore.CreateDirectory("test");

IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("test\\TEST.txt", System.IO.FileMode.Create, isoStore);

byte onlyOneByte = 101;

isoStream.WriteByte(onlyOneByte);

isoStream.Close();

isoStore.Dispose();

btnRead.IsEnabled = true;

txtShow.Text="写入完成";

//存储配置信息

var settings = IsolatedStorageSettings.ApplicationSettings;

//添加配置信息

settings.Add("ip", "192.168.1.1");

//保存

settings.Save();

}




读取操作


View
Code




/// <summary>

/// 读操作

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnRead_Click(object sender, RoutedEventArgs e)

{

IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();

if (isStore.FileExists("test\\TEST.txt"))

{

IsolatedStorageFileStream isStream = new IsolatedStorageFileStream("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读取完成";

//存储配置信息

var settings = IsolatedStorageSettings.ApplicationSettings;

//判断key 是否存在

if (settings.Contains("ip"))

{

//只支持key的查询

string ip = settings["ip"].ToString();

//out传参获取值

string ipStr;

settings.TryGetValue("ip", out ipStr);

txtShow.Text = txtShow.Text + "\n配置文件ip:" + ip + ";\n out传参ip:"+ipStr;

}

}

}




读取效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: