原文:与众不同 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(独立存储)的更多相关文章

  1. Silverlight-管理独立存储(Isolated Storage)

    Silverlight中的独立存储是其内部的可信任的可访问文件空间,在这里你可以使用Silverlight 随意的创建.读取.写入.删除目录和文件,它有一些类似于Cookie,但是它可以在客户端保存大 ...

  2. 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 ...

  3. Windows Phone 独立存储资源管理器工具

    如何使用独立存储资源管理器工具 http://msdn.microsoft.com/zh-CN/library/hh286408(v=vs.92)C:\Program Files (x86)\Micr ...

  4. Windows phone 之独立存储

    独立存储命名空间的说明:

  5. Windows Phone 独立存储查看器

    1.为了查看我们存放在独立存储的数据,我们需要借助独立存储查看器. 2.简单介绍下,IsoStoreSpy 下载地址:http://download.csdn.net/download/lhb1097 ...

  6. 与众不同 windows phone (2) - Control(控件)

    原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...

  7. 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件

    [源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...

  8. 与众不同 windows phone (15) - Media(媒体)之后台播放音频

    原文:与众不同 windows phone (15) - Media(媒体)之后台播放音频 [索引页][源码下载] 与众不同 windows phone (15) - Media(媒体)之后台播放音频 ...

  9. 与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成

    原文:与众不同 windows phone (14) - Media(媒体)之音频播放器, 视频播放器, 与 Windows Phone 的音乐和视频中心集成 [索引页][源码下载] 与众不同 win ...

随机推荐

  1. TreePuzzle 一点感想

    题目链接 这一道题看起来像是一道贪心的水题,但是情况较难考虑周全,比如下图,我就考虑漏了这种情况,点0是可以移动到点1的.然后我就各种奇怪的分类讨论.最终还是没能A,决定放弃治疗. 然后我看了看解答, ...

  2. Poj 2777 Count Color(线段树基础)

    又毁三观了.......虽然题目数据有坑:区间[a,b]可能会有a>b的情况,但是我一开始没有考虑它也能过. 此外莫名其妙的TLE #include <iostream> #incl ...

  3. Winform TabControl控件使用

    运行效果: 代码: /// <summary> /// 添加选项卡 /// </summary> /// <param name="sender"&g ...

  4. BZOJ 1131: [POI2008]Sta( dfs )

    对于一棵树, 考虑root的答案向它的孩子转移, 应该是 ans[son] = (ans[root] - size[son]) + (n - size[son]). so , 先 dfs 预处理一下, ...

  5. 基于visual Studio2013解决C语言竞赛题之0612递归

     题目

  6. ORA-19815,ORA-19809 :limit exceeded for recovery files

    数据库重新启动的时候,收到了ORA-19815的错误.从错误的提示来看,是由于闪回区的空间被填满导致无法成功启动.这种情形我们通常考虑的是清除归档日志,那就直接在OS层面rm了,真的是这样吗?客官,如 ...

  7. 使用数组实现队列----《数据结构与算法分析---C语言描述》

    一.h文件:my_que.h #ifndef _MY_QUE_H_ #define _MY_QUE_H_ struct QueRecord; typedef struct QueRecord* que ...

  8. Android应用开发经常使用知识

    在其它站点看到的,Mark一下 1.近期打开的应用不在近期任务列表中显示 android:excludeFromRecents="true" 设置为true,则排除在近期任务列表之 ...

  9. VC实现URL编解码器

    //变化UTF8为了中国 void UTF8ToGB(CString& szstr) { WCHAR* strSrc; TCHAR* szRes; int i = MultiByteToWid ...

  10. poj 3258 River Hopscotch 【二分】

    题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...