Universal App FolderPicker System.Runtime.InteropServices.COMException
Asked Answered
G

1

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

Gammadion answered 7/11, 2016 at 0:34 Comment(2)
have you tried it without using the async, await? If not, first try without async,await! most of the com exception getting thrown by the threads or you can say not using the threads in a proper wayBoyett
No solutions yet?Shamrock
G
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.

Gypsophila answered 26/2, 2017 at 10:3 Comment(5)
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context.Ungrounded
Sorry that I didn't make this clear, but it is actually an answer to the question.Gypsophila
Edited my answer accordingly.Gypsophila
Microsoft documentation uses picker.FileTypeFilter.Add("*") in this sampleClydesdale
@Clydesdale Thanks for the hint. I changed this in my answer.Gypsophila

© 2022 - 2024 — McMap. All rights reserved.