与众不同 windows phone (6) - Isolated Storage(独立存储)

时间:2023-03-09 01:46:52
与众不同 windows phone (6) - Isolated Storage(独立存储)

原文:与众不同 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
[源码下载]