设为首页 收藏本站
查看: 1308|回复: 0

重新想象 Windows 8 Store Apps (24)

[复制链接]

尚未签到

发表于 2015-5-22 09:57:18 | 显示全部楼层 |阅读模式
  [源码下载]




重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作  
作者:webabcd

介绍
重新想象 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















  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 这样写
//
        }
}
}
  
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














  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 中增加
StorageFolder removableDevices = KnownFolders.RemovableDevices; // 可移动存储设备
IReadOnlyList 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 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
[源码下载]

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-69439-1-1.html 上篇帖子: 重新想象 Windows 8 Store Apps (44) 下篇帖子: Windows Phone 8/Windows 8 启动第三方应用程序并传递参数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表