7

I'm trying to make a universal app which merely opens a folder (like a shortcut) but allows for the new start tile design with custom color and bigger icon.

When I open the FolderPicker to give the application access to the directory, I get an error, and I have no idea why.

System.Runtime.InteropServices.COMException

Please assist.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.AccessCache;
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 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();


            doPickFile();
            //Application.Current.Exit();
        }

        private async void doPickFile()
        {

            bool folderAdded = StorageApplicationPermissions.FutureAccessList.ContainsItem("\\\\server\\share$");

            if (!folderAdded)
            {
                var openPicker = new FolderPicker() { SuggestedStartLocation = PickerLocationId.PicturesLibrary  };
                StorageFolder folder = await openPicker.PickSingleFolderAsync();
                if (folder.Path == "\\\\server\\share$")
                {
                    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
                }
            }
            else
            {
                StorageFolder pickedFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("\\\\server\\share$");
                await Windows.System.Launcher.LaunchFolderAsync(pickedFolder);
            }
        }
    }
}

Specifically the debugger stops at the line: StorageFolder folder = await openPicker.PickSingleFolderAsync();

Simon
  • 165
  • 1
  • 8

1 Answers1

10

You have to add a FileTypeFilter.

    var openPicker = new FolderPicker() { SuggestedStartLocation = PickerLocationId.PicturesLibrary }; 
    openPicker.FileTypeFilter.Add("*");
    StorageFolder folder = await openPicker.PickSingleFolderAsync();

There is also an official Microsoft sample which shows this.

The strange thing about it: you could also use FileTypeFilter.Add(".anytext") instead of FileTypeFilter.Add("*") since the current FolderPicker in Windows does not actually filter for file types. Therefore I cannot explain why you have to do this.

Simon
  • 165
  • 1
  • 8
  • If you have a new question, please ask it by clicking the [Ask Question](/questions/ask) button. Include a link to this question if it helps provide context. – Donald Duck Feb 26 '17 at 10:19
  • 1
    Sorry that I didn't make this clear, but it _is_ actually an answer to the question. – Simon Feb 26 '17 at 10:58
  • Edited my answer accordingly. – Simon Feb 26 '17 at 11:15
  • Microsoft documentation uses `picker.FileTypeFilter.Add("*")` in [this sample](https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-using-file-and-folder-pickers#pick-a-folder-complete-code-listing) – ghord Mar 21 '19 at 16:56
  • @ghord Thanks for the hint. I changed this in my answer. – Simon Mar 22 '19 at 17:34