与众不同 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 ...
随机推荐
- 在Eclipse/MyEclipse中安装spket插件
Spket ide是强大的工具包为了JavaScript和XML的开发,这个强大的编辑器对JavaScript, XUL/XBLand Yahoo! Widget的开发都有全面的支持 ,比如代码完毕, ...
- QTP的基本功能介绍
• QTP的基本功能介绍 HP QuickTest Professional 支持功能測试和回归測试自己主动化,用于每一个主要软件应用程序和环境.此解决方式使用keyword驱动的測试概念,简化了測试 ...
- Oracle基础(五):多表查询
一.多表查询 (一)简单多表查询 1.多表查询的机制 1)SQL: SELECT * FROM emp; --14条记录 SELECT * FROM dept;--4条记录 SELECT * FROM ...
- [置顶] 深入浅出MongoDB(三)环境搭建
上次的博文深入浅出MongoDB(二)概述中我们已经将MongoDB的相关概念讲解了一下,接下来我们继续进行MongoDB学习.在学习之前,大家首先需要在自己的电脑上安装MongoDB. 1.安装 安 ...
- 三种LVS负载均衡技术的优缺点----负载均衡调度算法
三种LVS负载均衡技术的优缺点归纳以下表: VS/NATVS/TUNVS/DR 服务器操作系统任意支持隧道多数(支持Non-arp) 服务器网络私有网络局域网/广域网局域网 服务器数目(100M网络) ...
- 自己python程序的并行修改
遇到运算量大的程序,学习了下python并行运算的方法,在自己的程序上进行了修改,看看是否可以增加效率.原始代码是: import gt_apps as my_apps f=file('sample. ...
- haproxy /admin跳转 不会在接口上再次加上admin
http://www.xx.com/admin/api/menu [root@wx03 mojo]# cat test.pl use Mojolicious::Lite; use JSON qw/en ...
- android5.0(Lollipop) BLE Peripheral深入理解系统篇之提高篇
上一篇文章讲到了广播之前系统需要进行的准备工作,那接下来我们就来真正的启动广播. 首先还是先看一下上一篇文章结束的地方: @Override public void onClientRegistere ...
- 时间运算函数 CATT_ADD_TO_TIME
- 3890: [Usaco2015 Jan]Meeting Time( dp )
简单的拓扑图dp.. A(i, j), B(i, j) 表示从点 i 长度为 j 的两种路径是否存在. 用bitset就行了 时间复杂度O(m) --------------------------- ...