我正在尝试制作一个通用应用程序,它只打开一个文件夹(如快捷方式)但允许使用自定义颜色和更大图标的新开始磁贴设计。
当我打开 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/