Universal App FolderPicker System.Runtime.InteropServices.COMException

时间:2016-11-07 00:34:22

标签: c# uwp comexception

我正在尝试创建一个只打开文件夹(如快捷方式)的通用应用程序,但允许使用自定义颜色和更大图标的新启动平铺设计。

当我打开FolderPicker以授予应用程序访问目录的权限时,我收到错误,我不明白为什么。

  

System.Runtime.InteropServices.COMException

请协助。

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

具体来说,调试器在该行停止: StorageFolder folder = await openPicker.PickSingleFolderAsync();

1 个答案:

答案 0 :(得分:5)

您必须添加FileTypeFilter。

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

还有official Microsoft sample显示了这一点。

关于它的奇怪之处:您也可以使用FileTypeFilter.Add(".anytext")而不是FileTypeFilter.Add("*"),因为Windows中的当前FolderPicker实际上并不过滤文件类型。因此,我无法解释你为什么要这样做。