c# - 通用应用程序文件夹选择器 System.Runtime.InteropServices.COMException

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

最佳答案

您必须添加一个 FileTypeFilter。

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

还有一个 official Microsoft sample这表明了这一点。

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

关于c# - 通用应用程序文件夹选择器 System.Runtime.InteropServices.COMException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40456200/

相关文章:

c# - 在同一个 ListView 中拖放项目

c# - 如何在 UWP 应用程序中使用 Microsoft Graph 自动获取新电子邮件

c# - 在桌面上拖动文件时可能导致 COMExceptions 的原因是什么?

sql - Interop.SQLXMLBULKLOADLib.dll 问题

c# - 重定向 Microsoft 修补程序的标准输出

c# - 如何围绕 C# 4.0 任务所处理的数据创建闭包?

C#自定义控件(圆形进度条)Xamarin Forms

c# - 卡住 Openxml 中的 Pane 和列

c# - Windows UWP BluetoothLE设备缓存

ASP.NET 4.0 Web 应用程序抛出 "Incorrect function. (Exception from HRESULT: 0x80070001)"