作者:webabcd
介绍
重新想象 Windows 8 Store Apps 之 文件系统
示例
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
[源码下载]