與眾不同 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 / 1024 / 1024 + "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
[源碼下載]