文件系統: Application Data 中的文件操作, Package 中的文件操作
介紹
重新想象 Windows 8 Store Apps 之 文件系統
Application Data(應用程序數據存儲) 中的文件操作
Application Data(應用程序數據存儲) 中的“設置”操作
通過 uri 引用 Application Data(應用程序數據存儲) 中的媒體(圖片、視頻或音頻)
訪問 Package 中的文件
訪問可移動存儲
示例
1、演示如何在 Application Data(應用程序數據存儲)中對文件進行操作
FileSystem/AppData/FileDemo.xaml.cs
/*
* 演示如何在 Application Data(應用程序數據存儲)中對文件進行操作
*
* ApplicationData - 操作應用程序數據存儲的類
* Current - 返回當前的 ApplicationData 對象
* LocalFolder - 返回 StorageFolder 對象。本地存儲,永久保存
* 保存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\LocalState
* RoamingFolder - 返回 StorageFolder 對象。漫游存儲,同一微軟賬戶同一應用程序在不同設備間會自動同步
* 保存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\RoamingState
* TemporaryFolder - 返回 StorageFolder 對象。臨時存儲,系統在需要的時候可以將其刪除
* 保存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\TempState
* RoamingStorageQuota - 從漫游存儲同步到雲端的數據的最大大小,只讀
* ClearAsync() - 刪除 Application Data 中的數據 * ClearAsync(ApplicationDataLocality locality) - 刪除指定存儲區的數據據
* ApplicationDataLocality.Local, ApplicationDataLocality.Roaming, ApplicationDataLocality.Temporary
*
* DataChanged - 從服務端向 app 同步漫游數據時所觸發的事件
* SignalDataChanged() - 強行觸發 DataChanged 事件
*/
using System;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace XamlDemo.FileSystem.AppData
{
public sealed partial class FileDemo : Page
{
StorageFolder _roamingFolder = ApplicationData.Current.RoamingFolder;
public FileDemo()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ApplicationData.Current.DataChanged += Current_DataChanged;
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
ApplicationData.Current.DataChanged -= Current_DataChanged;
}
async void Current_DataChanged(ApplicationData sender, object args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DataChanged 事件被觸發";
});
}
private async void btnReadWrite_Click_1(object sender, RoutedEventArgs e)
{
// 寫
StorageFile fileWrite = await _roamingFolder.CreateFileAsync(@"webabcdTest\readWriteDemo.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(fileWrite, "I am webabcd: " + DateTime.Now.ToString());
// 讀
// StorageFile fileRead = await _roamingFolder.GetFileAsync(@"webabcdTest\readWriteDemo.txt");
// ms-appdata:///local/, ms-appdata:///roaming/, ms-appdata:///temp/
StorageFile fileRead = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///roaming/webabcdTest/readWriteDemo.txt", UriKind.Absolute));
string textContent = await FileIO.ReadTextAsync(fileRead);
lblMsg.Text = textContent;
// 強行觸發 DataChanged 事件(僅為演示用,實際項目中不需要)
ApplicationData.Current.SignalDataChanged();
}
}
}
2、演示如何在 Application Data(應用程序數據存儲)中對“設置”進行操作
FileSystem/AppData/SettingsDemo.xaml
<Page x:Class="XamlDemo.FileSystem.AppData.SettingsDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.FileSystem.AppData" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnReadWrite" Content="讀寫 Settings" Click="btnReadWrite_Click_1" Margin="0 10 0 0" /> <Button Name="btnReadWriteWithContainer" Content="分組 Settings" Click="btnReadWriteWithContainer_Click_1" Margin="0 10 0 0" /> <Button Name="btnReadWriteWithComposite" Content="父子 Settings" Click="btnReadWriteWithComposite_Click_1" Margin="0 10 0 0" /> <Button Name="btnSetVersion0" Content="將 Application Data 的版本號設置為 0" Click="btnSetVersion0_Click_1" Margin="0 10 0 0" /> <Button Name="btnSetVersion1" Content="將 Application Data 的版本號設置為 1" Click="btnSetVersion1_Click_1" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
FileSystem/AppData/SettingsDemo.xaml.cs
/*
* 演示如何在 Application Data(應用程序數據存儲)中對“設置”進行操作
*
* ApplicationData - 操作應用程序數據存儲的類
* Current - 返回當前的 ApplicationData 對象
* LocalSettings - 返回 ApplicationDataContainer 對象。本地存儲,永久保存
* 保存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\Settings
* RoamingSettings - 返回 ApplicationDataContainer 對象。漫游存儲,同一微軟賬戶同一應用程序在不同設備間會自動同步
* 保存路徑:%USERPROFILE%\AppData\Local\Packages\{PackageId}\Settings
* Version - 獲取當前 Application Data 的版本號,默認值為 0,只讀(用於本地“設置”數據的版本控制)
* SetVersionAsync() - 指定當前 Application Data 的版本號(用於本地“設置”數據的版本控制)
* * ApplicationDataContainer - 操作“設置”數據的類
* Name - 容器的名稱,默認為空 * CreateContainer(string name, ApplicationDataCreateDisposition disposition) - 激活一個用於保存“設置”數據的容器,即分組“設置”數據
* name - 容器的名稱
* disposition - 容器的激活方式:Always - 始終激活;Existing - 容器存在才激活
* Containers - 容器集合
* DeleteContainer() - 刪除指定的容器
* Values - 保存“設置”數據,一個字典表
* 其數據可以是一個 ApplicationDataCompositeValue 類型的數據,ApplicationDataCompositeValue 也是一個字典表,這樣可將多個“設置”數據放到一個 key 裡
* *
* 備注:
* 當 key 為 HighPriority 時,系統會以最快的速度在多個設備間同步 HighPriority 所對應的數據(支持 ApplicationDataCompositeValue 數據)
* 示例如下:
* ApplicationDataContainer.Values["HighPriority"] = "此處的值將會以系統最快的速度在多個設備間同步";
*/
using System;
using Windows.Storage;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace XamlDemo.FileSystem.AppData
{
public sealed partial class SettingsDemo : Page
{
public SettingsDemo()
{
this.InitializeComponent();
}
// 操作“設置”數據的 demo
private void btnReadWrite_Click_1(object sender, RoutedEventArgs e)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
// 保存“設置”數據
localSettings.Values["key"] = "I am webabcd";
// 如果 key 為 HighPriority,則系統將會以最高優先級的速度在多個設備間同步 HighPriority 的值
// localSettings.Values["HighPriority"] = "I am webabcd";
// 刪除指定的“設置”數據
// localSettings.Values.Remove("key");
// 獲取“設置”數據
lblMsg.Text = (string)localSettings.Values["key"];
}
// 分組“設置”數據,即在不同的容器中保存不同的數據
private void btnReadWriteWithContainer_Click_1(object sender, RoutedEventArgs e)
{
// 在 LocalSettings 中激活名為 groupName 的容器
ApplicationDataContainer container = ApplicationData.Current.LocalSettings;
ApplicationDataContainer localSettings = container.CreateContainer("groupName", ApplicationDataCreateDisposition.Always);
// 刪除指定的容器
// container.DeleteContainer("groupName");
// 在容器內保存“設置”數據
if (container.Containers.ContainsKey("groupName"))
{
container.Containers["groupName"].Values["key"] = "I am webabcd";
}
// 從指定的容器內獲取“設置”數據
lblMsg.Text = (string)container.Containers["groupName"].Values["key"];
lblMsg.Text += Environment.NewLine;
// 從指定的容器內獲取“設置”數據
lblMsg.Text += (string)localSettings.Values["key"];
}
// 父子“設置”數據,即 key 中的數據是一個 ApplicationDataCompositeValue 對象,而 ApplicationDataCompositeValue 也是一個字典表
private void btnReadWriteWithComposite_Click_1(object sender, RoutedEventArgs e)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
// 父子“設置”數據的保存
ApplicationDataCompositeValue parent1 = new ApplicationDataCompositeValue();
parent1["child1"] = "abc";
parent1["child2"] = "xyz";
localSettings.Values["parent1"] = parent1;
// 父子“設置”數據的獲取
lblMsg.Text = (string)((ApplicationDataCompositeValue)localSettings.Values["parent1"])["child1"];
lblMsg.Text += Environment.NewLine;
lblMsg.Text += (string)((ApplicationDataCompositeValue)localSettings.Values["parent1"])["child2"];
}
private async void btnSetVersion0_Click_1(object sender, RoutedEventArgs e)
{
// 將 Application Data 的版本號設置為 0,並執行指定的方法
await ApplicationData.Current.SetVersionAsync(0, new ApplicationDataSetVersionHandler(SetVersionHandler));
}
private async void btnSetVersion1_Click_1(object sender, RoutedEventArgs e)
{
// 將 Application Data 的版本號設置為 1,並執行指定的方法
await ApplicationData.Current.SetVersionAsync(1, new ApplicationDataSetVersionHandler(SetVersionHandler));
}
// 根據當前版本號和將要設置成的版本號,將“設置”數據升級到最新版本
async void SetVersionHandler(SetVersionRequest request)
{
// 異步操作
SetVersionDeferral deferral = request.GetDeferral();
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text = "CurrentVersion: " + request.CurrentVersion; // 當前版本號
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "DesiredVersion: " + request.DesiredVersion; // 將要設置成的版本號
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "ApplicationData.Current.Version: " + ApplicationData.Current.Version; // 當前版本號
});
// 完成異步操作
deferral.Complete();
}
}
}
3、演示如何通過 uri 的方式引用 Application Data(應用程序數據存儲)中的媒體(圖片、視頻或音頻 )文件
FileSystem/AppData/MediaReference.xaml.cs
/* * 演示如何通過 uri 的方式引用 Application Data(應用程序數據存儲)中的媒體(圖片、視頻或音頻)文件 * * ApplicationData - 操作應用程序數據存儲的類 * LocalFolder 對應 ms-appdata:///local/ * RoamingFolder 對應 ms-appdata:///roaming/ * TemporaryFolder 對應 ms-appdata:///temp/ */ using System; using Windows.Storage; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace XamlDemo.FileSystem.AppData { public sealed partial class MediaReference : Page { public MediaReference() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { ApplicationData appData = ApplicationData.Current; try { // 將程序包內的文件保存到 ApplicationData 中的 TemporaryFolder StorageFile imgFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Logo.png")); await imgFile.CopyAsync(appData.TemporaryFolder, imgFile.Name, NameCollisionOption.ReplaceExisting); } catch { } // 引用 Application Data 內的圖片文件並顯示 imgAppdata.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appdata:///temp/Logo.png")); // 引用程序包內的圖片文件並顯示 imgAppx.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/Logo.png")); // 也可以在 xaml 這樣寫 // <img src="ms-appdata:///temp/Logo.png" /> } } }
4、演示如何訪問 Package 中的文件
FileSystem/PackageData/Demo.xaml.cs
/* * 演示如何訪問 Package 中的文件 */ using System; using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.FileSystem.PackageData { public sealed partial class Demo : Page { public Demo() { this.InitializeComponent(); } private async void btnReadWrite_Click_1(object sender, RoutedEventArgs e) { // 寫(無法向程序包中寫數據,會報錯) // StorageFile fileWrite = await Package.Current.InstalledLocation.CreateFileAsync (@"FileSystem\PackageData\readWriteDemo.txt", CreationCollisionOption.ReplaceExisting); // await FileIO.WriteTextAsync(fileWrite, "I am webabcd: " + DateTime.Now.ToString()); // 讀 // StorageFile fileRead = await Package.Current.InstalledLocation.GetFileAsync(@"FileSystem\PackageData\readWriteDemo.txt"); StorageFile fileRead = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///FileSystem/PackageData/readWriteDemo.txt", UriKind.Absolute)); string textContent = await FileIO.ReadTextAsync(fileRead); lblMsg.Text = textContent; // 引用程序包內的圖片文件並顯示 img.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/Logo.png")); } } }
5、演示如何訪問可移動存儲
FileSystem/RemovableDevice/Demo.xaml
<Page x:Class="XamlDemo.FileSystem.RemovableDevice.Demo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.FileSystem.RemovableDevice" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" TextWrapping="Wrap" /> <Button Name="btnGetRemovableDevices" Content="獲取全部可移動存儲" Margin="0 10 0 0" Click="btnGetRemovableDevices_Click" /> <Button Name="btnWrite" Content="write" Margin="0 10 0 0" Click="btnWrite_Click" /> <Button Name="btnRead" Content="read" Margin="0 10 0 0" Click="btnRead_Click" /> <Image Name="img" Stretch="None" HorizontalAlignment="Left" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
FileSystem/RemovableDevice/Demo.xaml.cs
/* * 演示如何訪問可移動存儲 */ using System; using System.Collections.Generic; using Windows.Devices.Enumeration; using Windows.Devices.Portable; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; namespace XamlDemo.FileSystem.RemovableDevice { public sealed partial class Demo : Page { public Demo() { this.InitializeComponent(); } private async void btnGetRemovableDevices_Click(object sender, RoutedEventArgs e) { lblMsg.Text += "可用的移動存儲如下:"; lblMsg.Text += Environment.NewLine; // 要在 Package.appxmanifest 中增加 <Capability Name="removableStorage" /> StorageFolder removableDevices = KnownFolders.RemovableDevices; // 可移動存儲設備 IReadOnlyList<StorageFolder> removableStorages = await removableDevices.GetFoldersAsync(); // 每個可移動存儲設備的根目錄 if (removableStorages.Count > 0) { foreach (StorageFolder storageFolder in removableStorages) { // 顯示每個可移動存儲設備的名稱 lblMsg.Text += storageFolder.DisplayName; lblMsg.Text += Environment.NewLine; // 顯示該可移動存儲設備根目錄下的文件數量(本 app 有權訪問的文件類型的文件數量) // 注:如果需要讀寫某擴展名的文件,需要在 Package.appxmanifest 增加“文件類型關聯”聲明,並做相應的配置 IReadOnlyList<StorageFile> storageFiles = await storageFolder.GetFilesAsync(); lblMsg.Text += "files count: " + storageFiles.Count; lblMsg.Text += Environment.NewLine; } } else { lblMsg.Text += "未找到可用的移動存儲"; lblMsg.Text += Environment.NewLine; } } private async void btnWrite_Click(object sender, RoutedEventArgs e) { // 關於 DeviceInformation 請參見:Information/DeviceInfo.xaml DeviceInformationCollection interfaces = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); if (interfaces.Count > 0) { try { // 找到第一個可移動存儲設備(也可以通過 KnownFolders.RemovableDevices 來獲取) DeviceInformation deviceInformation = interfaces[0]; // 獲取第一個可移動存儲設備的根目錄 StorageFolder storageFolder = StorageDevice.FromId(deviceInformation.Id); // 注:如果需要讀寫某擴展名的文件,需要在 Package.appxmanifest 增加“文件類型關聯”聲明,並做相應的配置 StorageFile imgFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Logo.png")); // 將文件復制到第一個可移動存儲設備的根目錄 await imgFile.CopyAsync(storageFolder, "Logo.png", NameCollisionOption.ReplaceExisting); lblMsg.Text += "已將文件 Logo.png 復制到了 " + storageFolder.Name; lblMsg.Text += Environment.NewLine; } catch (Exception ex) { lblMsg.Text += ex.ToString(); lblMsg.Text += Environment.NewLine; } } else { lblMsg.Text += "未找到可用的移動存儲"; lblMsg.Text += Environment.NewLine; } } private async void btnRead_Click(object sender, RoutedEventArgs e) { // 關於 DeviceInformation 請參見:Information/DeviceInfo.xaml DeviceInformationCollection interfaces = await DeviceInformation.FindAllAsync(StorageDevice.GetDeviceSelector()); if (interfaces.Count > 0) { try { // 找到第一個可移動存儲設備(也可以通過 KnownFolders.RemovableDevices 來獲取) DeviceInformation deviceInformation = interfaces[0]; // 獲取第一個可移動存儲設備的根目錄 StorageFolder storageFolder = StorageDevice.FromId(deviceInformation.Id); // 在第一個可移動存儲設備的根目錄中獲取指定的文件 StorageFile storageFile = await storageFolder.GetFileAsync("Logo.png"); // 顯示圖片 IRandomAccessStream imageStream = await storageFile.OpenReadAsync(); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(imageStream); img.Source = bitmapImage; lblMsg.Text += "已從 " + storageFolder.Name + " 獲取到了 Logo.png 文件"; lblMsg.Text += Environment.NewLine; } catch (Exception ex) { lblMsg.Text += "沒有找到 Logo.png 文件。" + ex.ToString(); lblMsg.Text += Environment.NewLine; } } else { lblMsg.Text += "未找到可用的移動存儲"; lblMsg.Text += Environment.NewLine; } } } }
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar