背水一戰 Windows 10 (90) - 文件系統: 獲取 Package 中的文件, 可移動存儲中的文件操作, “庫”管理
阿新 • • 發佈:2018-06-14
作者 tsl ide RR str XA hololens 註意 ica
[源碼下載]
作者: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 中的文件, 可移動存儲中的文件操作, “庫”管理