[源码下载]

背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

作者:webabcd

介绍
背水一战 Windows 10 之 文件系统

  • 获取 Package 中的文件
  • 可移动存储中的文件操作
  • “库”管理

示例
1、演示如何获取 Package 中的文件
FileSystem/PackageData/Demo.xaml

<Page
x:Class="Windows10.FileSystem.PackageData.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.FileSystem.PackageData"
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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnRead" Content="读取 Package 中的文件" Click="btnRead_Click" Margin="5" /> <Image Name="img" Stretch="None" HorizontalAlignment="Left" Margin="5" /> </StackPanel>
</Grid>
</Page>

FileSystem/PackageData/Demo.xaml.cs

/*
* 演示如何获取 Package 中的文件
* 1、可以通过 Package.Current.InstalledLocation.GetFileAsync() 访问
* 2、可以通过 ms-appx:/// 访问
*
*
* StorageFile - 文件操作类
* public static IAsyncOperation<StorageFile> GetFileFromApplicationUriAsync(Uri uri) - 获取本地指定 uri 的文件
*
*
* 注:需要访问的 Package 中的文件的属性的“生成操作”应该设置为“内容”
*/ using System;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem.PackageData
{
public sealed partial class Demo : Page
{
public Demo()
{
this.InitializeComponent();
} private async void btnRead_Click(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 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/hololens.jpg"));
}
}
}

2、演示如何在可移动存储中对文件进行操作
FileSystem/RemovableDevice/Demo.xaml

<Page
x:Class="Windows10.FileSystem.RemovableDevice.Demo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <ListBox Name="listBox" Width="400" Height="200" HorizontalAlignment="Left" Margin="5" SelectionChanged="listBox_SelectionChanged" /> <Button Name="btnCopyFile" Content="复制文件到当前选中的设备中" Margin="5" Click="btnCopyFile_Click" /> </StackPanel>
</Grid>
</Page>

FileSystem/RemovableDevice/Demo.xaml.cs

/*
* 演示如何在可移动存储中对文件进行操作
*/ using System;
using System.Collections.Generic;
using System.Linq;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.FileSystem.RemovableDevice
{
public sealed partial class Demo : Page
{
public Demo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取全部可移动存储
StorageFolder removableDevices = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.RemovableDevices);
IReadOnlyList<StorageFolder> folderList = await removableDevices.GetFoldersAsync();
listBox.ItemsSource = folderList.Select(p => p.Name).ToList(); base.OnNavigatedTo(e);
} private async void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 用户选中的可移动存储
string folderName = (string)listBox.SelectedItem;
StorageFolder removableDevices = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.RemovableDevices);
StorageFolder storageFolder = await removableDevices.GetFolderAsync(folderName); // 用户选中的可移动存储中的根目录下的文件和文件夹总数
IReadOnlyList<IStorageItem> storageItems = await storageFolder.GetItemsAsync();
lblMsg.Text = "items count: " + storageItems.Count;
} private async void btnCopyFile_Click(object sender, RoutedEventArgs e)
{
// 用户选中的可移动存储
string folderName = (string)listBox.SelectedItem; if (folderName != null)
{
StorageFolder removableDevices = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.RemovableDevices);
StorageFolder storageFolder = await removableDevices.GetFolderAsync(folderName); // 复制包内文件到指定的可移动存储
StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/hololens.jpg"));
try
{
await storageFile.CopyAsync(storageFolder, "hololens.jpg", NameCollisionOption.ReplaceExisting);
lblMsg.Text = "复制成功";
}
catch (Exception ex)
{
lblMsg.Text = ex.ToString();
}
}
}
}
}

3、演示如何 添加/删除 “库”所包含的文件夹
FileSystem/StorageLibrary/StorageLibraryDemo.xaml

<Page
x:Class="Windows10.FileSystem.StorageLibrary.StorageLibraryDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.FileSystem.StorageLibrary"
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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnAddFolder" Content="增加一个文件夹引用到图片库" Click="btnAddFolder_Click" Margin="5" /> <Button Name="btnRemoveFolder" Content="从图片库移除之前添加的全部文件夹引用" Click="btnRemoveFolder_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

FileSystem/StorageLibrary/StorageLibraryDemo.xaml.cs

/*
* 演示如何 添加/删除 “库”所包含的文件夹
*
* StorageLibrary - 用于“库”管理
* GetLibraryForUserAsync(User user, KnownLibraryId libraryId) - 获取指定用户的指定“库”,返回 StorageLibrary 类型的对象
* user - 指定用户,传 null 则为当前用户(关于 User 相关请参见 /UserAndAccount/UserInfo.xaml)
* libraryId - 指定库目录,一个 KnownLibraryId 类型的枚举值(Music, Pictures, Videos, Documents)
* Folders - 当前库所包含的文件夹们
* SaveFolder - 当前库的默认文件夹
* RequestAddFolderAsync() - 添加文件夹到当前库
* RequestRemoveFolderAsync() - 从当前库移除指定的文件夹
* DefinitionChanged - 当前库所包含的文件夹发生变化时触发的事件
*
*
* 注:根据需要请在 Package.appxmanifest 中配置 <Capability Name="picturesLibrary" />, <Capability Name="videosLibrary" />, <Capability Name="musicLibrary" />, <Capability Name="documentsLibrary" />
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.FileSystem.StorageLibrary
{
public sealed partial class StorageLibraryDemo : Page
{
// 临时保存添加进图片库的文件夹
private List<StorageFolder> _addedFloders = new List<StorageFolder>(); public StorageLibraryDemo()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 注意:要想访问图片库,别忘了增加 <Capability Name="picturesLibrary" /> // 获取图片库的 StorageLibrary 对象
Windows.Storage.StorageLibrary picturesLibrary = await Windows.Storage.StorageLibrary.GetLibraryForUserAsync(null, KnownLibraryId.Pictures); // 当前库所包含的文件夹增多或减少时
picturesLibrary.DefinitionChanged += async (Windows.Storage.StorageLibrary innerSender, object innerEvent) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text = "图片库所包含的文件夹如下:";
foreach (StorageFolder folder in picturesLibrary.Folders) // 当前库所包含的全部文件夹
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += folder.Path;
}
});
}; base.OnNavigatedTo(e);
} // 增加一个文件夹引用到图片库
private async void btnAddFolder_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.StorageLibrary picturesLibrary = await Windows.Storage.StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); // 弹出文件夹选择器,以选择需要添加到图片库的文件夹
StorageFolder addedFolder = await picturesLibrary.RequestAddFolderAsync();
if (addedFolder != null)
{
// 添加成功
_addedFloders.Add(addedFolder);
}
else
{ }
} // 从图片库移除之前添加的全部文件夹引用
private async void btnRemoveFolder_Click(object sender, RoutedEventArgs e)
{
Windows.Storage.StorageLibrary picturesLibrary = await Windows.Storage.StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); foreach (StorageFolder folder in _addedFloders)
{
// 从图片库移除指定的文件夹引用
if (await picturesLibrary.RequestRemoveFolderAsync(folder))
{
// 移除成功
}
else
{ }
}
}
}
}

OK
[源码下载]

背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理的更多相关文章

  1. 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件

    [源码下载] 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件 作者:webabcd 介绍背水一战 Windows 10 之 ...

  2. 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图

    [源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...

  3. 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图

    [源码下载] 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获取文件夹的属性 ...

  4. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  5. 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引

    [源码下载] 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引 作者:webabcd 介绍背水一战 Windows 10 之 文件系 ...

  6. 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据

    [源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...

  7. 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件

    [源码下载] 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 创建文件夹,重命名文件夹,删除文件夹, ...

  8. 背水一战 Windows 10 (4) - UI: 多窗口

    [源码下载] 背水一战 Windows 10 (4) - UI: 多窗口 作者:webabcd 介绍背水一战 Windows 10 之 UI 多窗口 示例1.自定义帮助类,用于简化 Secondary ...

  9. 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素

    [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...

随机推荐

  1. Dubbo注册Zookepper服务的虚拟IP

    使用dubbo在zookepper上注册服务,使用dubbo的服务器IP为192.168.70.105 而在zookepper上显示服务提供者为 dubbo://202.102.110.203:808 ...

  2. 源码阅读经验谈-slim,darknet,labelimg,caffe(1)

    本文首先谈自己的源码阅读体验,然后给几个案例解读,选的例子都是比较简单.重在说明我琢磨的点线面源码阅读方法.我不是专业架构师,是从一个深度学习算法工程师的角度来谈的,不专业的地方请大家轻拍. 经常看别 ...

  3. SpringBoot 试手(简易的SpringBoot搭建步骤)

    SpringBoot 也算AI吧,它根据您架构中引用的依赖,自动化地按默认方案帮您完成了Spring那些复杂繁琐的配置工作.为了让您不会看低此 AI 水平,还特地喊出了“约定大于配置”的口号.从这个角 ...

  4. 远程桌面 把explorer关掉了

    用Ctrl+Alt+End调出远程桌面的任务管理器.然后,运行explorer.exe即可重启该服务.

  5. iptables随笔

    iptables 分为四表五链 四表: filter表 nat 表 mangle 表 raw 表 五链 INPUT 链 OUTPUT 链 FORWARD 链 PREROUTING(路由前) POSTR ...

  6. C# 字符串 输出格式 指定间隔 通用性很强

    C#winform string s = "FE 68 01 00 1111 11 11 68 1104 35 33B337 7C 16"; string r = Regex.Re ...

  7. db2常见命令

    增加db2top命令的refresh间隔,默认值为2秒,下面的命令就可以每10秒刷新一次: $ db2top -i 10 -d sample 数据库本身太繁忙(dynamic SQL过多).建议增加 ...

  8. 20175126《Java程序设计》第一周学习总结

    # 学号 20175126 <Java程序设计>第一周学习总结   ## 教材学习内容总结   - 1.安装了WINDOS系统的JDK,并学会了利用JDK编写并编译JAVA程序的基本方法. ...

  9. jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小

    由jQuery实现上下.左右动态改变左右.上下两个div的大小,需要自己引入jquery1.8.0.min.js包 可用于页面布局. //============================ind ...

  10. Centos Firefox中文乱码

    解决CentOS Firefox 中文乱码问题,执行以下命令 Centos 6 # yum -y groupinstall chinese-support 重启电脑即可. Centos 7 yum - ...