与众不同 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 ...
随机推荐
- IntelliJ idea 14 集成 tomcat 7
来到公司第一个任务就是先把web项目跑起来,所以如何在IntelliJ idea14中把Tomcat集成起来也是个不小的问题. 首先确认你的idea 14 不是 Communit Edition 社区 ...
- 使用Maven构建和部署J2EE应用程序的EAR文件
这篇文章.主要是技术上的整理,用来mark一下,用的时候參考. 一.新建项目 新建一个空的Maven Project项目 二.放入依赖 注:ear部署时假设里面有entity,会错误发生.所以不要把e ...
- 利用Java内置的API开发JMX功能
一.什么是JMX JMS是一种Java规范,定义了如何管理一个软件系统(或应用程序)的规范. 对于一个简单的应用程序,该程序本身不需要被管理.但如果是开发的一个复杂系统(如一个电商平台.一个企业内部管 ...
- linux下的软件包安装
linux下安装软件包有两种方法:源文件编译安装(source)和 rpm 安装. 1.源文件包安装的通用方法. 一般安装源代码的程序你得要看它的README,一般在它的目录下都有的. 01.配置: ...
- anroid里面的post请求
一.需要用到的场景 在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了. 二.需要用到的主要类 在a ...
- 在Web Api中快速实现JSonp
本文翻译自:http://www.codeproject.com/Tips/631685/JSONP-in-ASP-NET-Web-API-Quick-Get-Started Concept: 同源策 ...
- BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )
水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...
- Android 点亮屏幕
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 基于visual Studio2013解决面试题之1003字符串逆序
题目
- C++数据结构之二叉树
之前打算编算法类的程序,但是搞了几次英雄会后,觉得作为一个还在学习阶段的学生,实在是太浪费时间了,并不是没意义,而是我的基础还不牢固啊.所以转变了思路,这个学期打算分别用C++.Python.Java ...