Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal App FolderPicker System.Runtime.InteropServices.COMException

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();

like image 319
Sean O'Leary Avatar asked Nov 07 '16 00:11

Sean O'Leary


1 Answers

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.

like image 83
Simon Avatar answered Nov 15 '22 20:11

Simon