From 2c40a1412bdb20a6a79be4013af9f3dda948c357 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Tue, 1 Feb 2022 22:27:39 +0200 Subject: [PATCH] Implement simple file picker poc for UWP --- UwpApplication/MainPage.xaml | 9 +++++++- UwpApplication/MainPage.xaml.cs | 38 ++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/UwpApplication/MainPage.xaml b/UwpApplication/MainPage.xaml index d0e1023..ee14608 100644 --- a/UwpApplication/MainPage.xaml +++ b/UwpApplication/MainPage.xaml @@ -9,6 +9,13 @@ Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> - + + + test 1 + test 2 + test 3 + test 4 + + diff --git a/UwpApplication/MainPage.xaml.cs b/UwpApplication/MainPage.xaml.cs index 09eec0e..52d9535 100644 --- a/UwpApplication/MainPage.xaml.cs +++ b/UwpApplication/MainPage.xaml.cs @@ -1,17 +1,7 @@ using System; using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; -using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -25,6 +15,34 @@ namespace UwpApplication public MainPage() { this.InitializeComponent(); + Console.WriteLine("Main Page"); + this.Loaded += LoadedHandler; + } + + private async void LoadedHandler(object sender, RoutedEventArgs e) + { + var folderPicker = new Windows.Storage.Pickers.FolderPicker(); + folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop; + folderPicker.FileTypeFilter.Add("*"); + + Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync(); + if (folder != null) + { + // Application now has read/write access to all contents in the picked folder + // (including other sub-folder contents) + Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); + + List fileNames = new List(); + var folderItems = await folder.GetItemsAsync(); + foreach (var item in folderItems) + { + if (item.Attributes == Windows.Storage.FileAttributes.Archive || item.Attributes == Windows.Storage.FileAttributes.Normal) + { + fileNames.Add(item.Name); + } + } + FilesListView.ItemsSource = fileNames; + } } } }