与众不同 windows phone (6) - Isolated Storage(独立存储)
原文:与众不同 windows phone (6) - Isolated Storage(独立存储)
作者:webabcd
介绍
与众不同 windows phone 7.5 (sdk 7.1) 之独立存储
- 概述
- 独立存储的读/写的Demo
- 读/写 key/value 形式数据到独立存储的快捷方法
示例
1、概述
Summary.xaml
<phone:PhoneApplicationPage
x:Class="Demo.IsolatedStorageDemo.Summary"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<ScrollViewer>
<TextBlock TextWrapping="Wrap">
<Run>Isolated Storage 概述</Run>
<LineBreak />
<LineBreak />
<Run>通过 IsolatedStorageFile 操作独立存储;通过 IsolatedStorageSettings 可方便地在独立存储中操作 key/value 形式的数据</Run>
<LineBreak />
<LineBreak />
<Run>独立存储内的特殊用途的文件夹</Run>
<LineBreak />
<Run>1、Shared/Media - 保存专辑封面</Run>
<LineBreak />
<Run>2、Shared/ShellContent - 保存 tile 的背景图</Run>
<LineBreak />
<Run>3、Shared/Transfers - 用于保存后台传输任务的 上传/下载 数据</Run>
<LineBreak />
<LineBreak />
<Run>独立存储资源管理器的使用,该工具在类似如下的地址 C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool\ISETool.exe</Run>
<LineBreak />
<Run>1、显示根目录下的目录及文件列表 ISETool.exe dir xd 0fb9e5a3-d4e0-4b0f-b56e-a347bfda0480(id 为 ProductId,可在 WMAppManifest.xml 中找到)</Run>
<LineBreak />
<Run>2、显示指定目录下的目录及文件列表 ISETool.exe dir:"Folder" xd 0fb9e5a3-d4e0-4b0f-b56e-a347bfda0480</Run>
<LineBreak />
<Run>3、从独立存储复制数据到计算机 ISETool.exe ts xd 0fb9e5a3-d4e0-4b0f-b56e-a347bfda0480 "C:\MyData"(会在此目录下创建一个名为 IsolatedStore 的子目录)</Run>
<LineBreak />
<Run>4、从计算机复制数据到独立存储 ISETool.exe rs xd 0fb9e5a3-d4e0-4b0f-b56e-a347bfda0480 "C:\MyData\IsolatedStore"</Run>
<LineBreak />
<LineBreak />
<Run>在多线程操作独立存储的场景下,建议使用互斥锁,即 System.Threading.Mutex</Run>
<LineBreak />
<LineBreak />
<Run>温馨小提示:appdata:/ 代表程序包内;isostore:/ 代表独立存储。默认为独立存储</Run>
</TextBlock>
</ScrollViewer>
</Grid> </phone:PhoneApplicationPage>
2、演示如何 读/写 独立存储
ReadWriteDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.IsolatedStorageDemo.ReadWriteDemo"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <TextBlock x:Name="lblMsg" /> <Button x:Name="btnWrite" Content="写入" Click="btnWrite_Click" /> <Button x:Name="btnRead" Content="读取" Click="btnRead_Click" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>
ReadWriteDemo.xaml.cs
/*
* Isolated Storage - 独立存储
*
* IsolatedStorageFile - 操作 独立存储 的类
* IsolatedStorageFile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储
*
* DirectoryExists(path) - 指定的路径是否存在
* CreateDirectory(path) - 创建指定的路径
* FileExists(path) - 指定的文件是否存在
* CreateFile(path) - 创建指定的文件
* GetDirectoryNames() - 获取根目录下的目录名数组
* GetFileNames()() - 获取根目录下的文件名数组
* GetDirectoryNames(path) - 获取指定目录下的目录名数组
* GetFileNames(path) - 获取指定目录下的文件名数组
* GetCreationTime(path) - 返回指定文件夹或文件的创建时间
* GetLastAccessTime(path) - 返回指定文件夹或文件最近一次被访问的时间
* GetLastWriteTime(path) - 返回指定文件夹或文件最近一次被写入内容的时间
* OpenFile() - 打开指定的文件。具体参数参看文档
* CopyFile(String, String, Boolean) - 复制文件,可以指定是否覆盖已有文件
* MoveDirectory() - 移动文件夹
* MoveFile() - 移动文件
* DeleteFile(path) - 删除指定的文件
* DeleteDirectory(path) - 删除指定的目录(要求目录存在,且目录内无内容)
* Remove() - 关闭 IsolatedStorageFile 对象并移除独立存储内的全部内容
*
* AvailableFreeSpace - 独立存储目前的可用空间
* Quota - 配额,即程序上允许的最大可用空间(wp7中这个属性没什么用,因为没有配额限制)
* IncreaseQuotaTo() - 请求允许一个更大的配额(wp7中这个属性没什么用,因为没有配额限制)
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using System.IO.IsolatedStorage;
using System.IO; namespace Demo.IsolatedStorageDemo
{
public partial class ReadWriteDemo : PhoneApplicationPage
{
public ReadWriteDemo()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(TextReadWrite_Loaded);
} void TextReadWrite_Loaded(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
lblMsg.Text = "可用空间:" + isf.AvailableFreeSpace / / + "MB";
} private void btnWrite_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); isf.CreateDirectory("Folder"); // IsolatedStorageFileStream - 独立存储内的文件流。继承自 FileStream
using (var isfs = new IsolatedStorageFileStream(@"Folder\File.txt", FileMode.OpenOrCreate, isf))
{
using (var sw = new StreamWriter(isfs))
{
sw.WriteLine("hello webabcd");
}
}
} private void btnRead_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); try
{
// IsolatedStorageFileStream - 独立存储内的文件流。继承自 FileStream
using (var isfs = new IsolatedStorageFileStream(@"Folder\File.txt", FileMode.Open, isf))
{
using (var sr = new StreamReader(isfs))
{
MessageBox.Show(sr.ReadLine());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
3、演示读/写 key/value 形式数据到独立存储的快捷方法
IsolatedStorageSettingsDemo.xaml
<phone:PhoneApplicationPage
x:Class="Demo.IsolatedStorageDemo.IsolatedStorageSettingsDemo"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"> <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel Orientation="Vertical"> <Button x:Name="btnWrite" Content="写入" Click="btnWrite_Click" /> <Button x:Name="btnRead" Content="读取" Click="btnRead_Click" /> </StackPanel>
</Grid> </phone:PhoneApplicationPage>
IsolatedStorageSettingsDemo.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls; using System.IO.IsolatedStorage; namespace Demo.IsolatedStorageDemo
{
public partial class IsolatedStorageSettingsDemo : PhoneApplicationPage
{
public IsolatedStorageSettingsDemo()
{
InitializeComponent();
} private void btnWrite_Click(object sender, RoutedEventArgs e)
{
/*
* IsolatedStorageSettings - 用非常方便的方法在独立存储中保存 key/value 形式的数据
* IsolatedStorageSettings.ApplicationSettings - 按应用程序保存 key/value 数据
*
* IsolatedStorageSettings 实现的接口有:IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IDictionary, ICollection, IEnumerable
*/ IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings; // 用这种方法保存 key/value 形式的数据非常简单
iss["abc"] = "webabcd"; // 保存 IsolatedStorageSettings 数据,但是经过测试,即使不调用此方法,数据也会被保存。但是为了保险还是调用一下 Save() 比较好
iss.Save();
} private void btnRead_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings; // 用这种方法读取 key/value 形式的数据非常简单
if (iss.Contains("abc"))
MessageBox.Show((string)iss["abc"]);
}
}
}
OK
[源码下载]
与众不同 windows phone (6) - Isolated Storage(独立存储)的更多相关文章
- Silverlight-管理独立存储(Isolated Storage)
Silverlight中的独立存储是其内部的可信任的可访问文件空间,在这里你可以使用Silverlight 随意的创建.读取.写入.删除目录和文件,它有一些类似于Cookie,但是它可以在客户端保存大 ...
- How to use the Isolated Storage Explorer tool for Windows Phone
Isolated Storage Explorer is installed in the following location: Program Files (x86)\Microsoft SDKs ...
- Windows Phone 独立存储资源管理器工具
如何使用独立存储资源管理器工具 http://msdn.microsoft.com/zh-CN/library/hh286408(v=vs.92)C:\Program Files (x86)\Micr ...
- Windows phone 之独立存储
独立存储命名空间的说明:
- Windows Phone 独立存储查看器
1.为了查看我们存放在独立存储的数据,我们需要借助独立存储查看器. 2.简单介绍下,IsoStoreSpy 下载地址:http://download.csdn.net/download/lhb1097 ...
- 与众不同 windows phone (2) - Control(控件)
原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...
- 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件
[源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...
- 与众不同 windows phone (15) - Media(媒体)之后台播放音频
原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...
- 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成
原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...
随机推荐
- yii框架网址解析问题
转载请注明来自souldak,微博:@evagle 首先如果你在config/main.php里面没有配置urlManager的话,那么流程如下(摘自yii官网) 用户发出了访问 URL http:/ ...
- eclipse中build workspace的相关优化
网上流传的各种的eclipse的调优的方法都大同小异,但是调优的基本上针对eclipse或者myclipse的本身,比如关掉validate和启动项,文件拼写,和自动构建等,调过之后,等个eclips ...
- leetcode第一刷_Maximal Rectangle
这个题比刚才那个更难. 假设没做过上一个,这个简直是无情. 先想一个笨笨的解法,如何确定一个矩形呢?找一个左上角,然后每行的看能延伸到什么位置.注意随着行数的添加,列数是仅仅能变短,不能变长. 想一下 ...
- 隐式意图-activity
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW);//设置动作 intent.setData(Uri.parse(& ...
- ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);
1 <h2>Welcome</h2> <ul> <% for (int i = 0; i <= Convert.ToInt32(ViewData[&qu ...
- BZOJ 4059: [Cerc2012]Non-boring sequences ( )
要快速在一段子序列中判断一个元素是否只出现一次 , 我们可以预处理出每个元素左边和右边最近的相同元素的位置 , 这样就可以 O( 1 ) 判断. 考虑一段序列 [ l , r ] , 假如我们找到了序 ...
- html submit 登录
<!doctype html> <html lang="en"> <head> <meta name="Generator&qu ...
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
今天布置环境的时候发现一个问题:Your working copy is out of date. Try pulling from the remote to get the latest chan ...
- SPOJ 11840. Sum of Squares with Segment Tree (线段树,区间更新)
http://www.spoj.com/problems/SEGSQRSS/ SPOJ Problem Set (classical) 11840. Sum of Squares with Segme ...
- 给你的Cordova HybridApp加入Splash启动页面
如今最新的Cordova 3以上的版本号支持启动画面了,是通过cordova插件实现的. 眼下Splash插件支持android,ios,blackberry等多个平台. 加入插件等步骤例如以下: 加 ...