代码之家  ›  专栏  ›  技术社区  ›  Steve Fitzsimons

如何在WPF c#桌面应用程序中使用UWP FolderPicker

  •  1
  • Steve Fitzsimons  · 技术社区  · 8 年前

    我有一个WPF桌面应用程序,我想使用UWP FolderPicker API来选择一个目录。我的应用程序使用UWP打包项目,因此它是作为一个appx构建和运行的。我已经添加了Windows和WindowsBase引用,以及我的项目生成和运行。但是,在尝试使用文件夹选择器时,我遇到了一个运行时错误。我的代码如下:

        private async void OnGetDirectory(object parameter)
        {
    
            var folderPicker = new Windows.Storage.Pickers.FolderPicker();
            folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
            folderPicker.FileTypeFilter.Add("*");
    
            Windows.Storage.StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
                // Application now has read/write access to all contents in the picked folder
                // (including other sub-folder contents)
                Windows.Storage.AccessCache.StorageApplicationPermissions.
                FutureAccessList.AddOrReplace("PickedFolderToken", folder);
            }
            else
            {
            }
        }
    

    我得到的错误是在线系统上。例外情况: await folderPicker.PickSingleFolderAsync(); 错误是 Invalid window handle. (Exception from HRESULT: 0x80070578)'

    2 回复  |  直到 8 年前
        1
  •  0
  •   Sunteen Wu    8 年前

    而不是UWP FolderPicker 您可以使用普通的Windows窗体文件夹对话框- FolderBrowserDialog System.Windows.Forms this question 例如)。

    StorageFolder ,你可以打电话 StorageFolder.GetFolderFromPathAsync 方法,也可以使用经典的文件夹API,如 System.IO.Directory System.IO.DirectoryInfo

        2
  •  1
  •   Martin Zikmund    8 年前

    根据 UWP APIs available to a packaged desktop app (Desktop Bridge)

    打包的应用程序可以完全访问文件系统,不需要UWP选择器。

    FolderPicker 您的UWP打包项目可能不支持完成。请尝试使用WPF本身支持的文件相关API。

    更多详情请参考 this document