You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#的FolderBrowserDialog中显示「快速访问」窗格?

实现带「快速访问」窗格的文件夹选择对话框

传统的FolderBrowserDialog是旧版系统对话框,不支持显示快速访问面板。要实现和OpenFileDialog一致的快速访问功能,可采用以下两种方案:

方案一:.NET 5+ 内置FolderPicker(推荐)

从.NET 5开始,可直接使用原生支持快速访问的Windows.Storage.Pickers.FolderPicker,适配现代系统对话框样式。

修改后代码示例

/// <summary>
/// 浏览并选择文件夹的事件处理方法
/// </summary>
/// <param name="mopParameter">参数对象</param>
/// <param name="rowIndex">DataGridView中正在编辑的行索引</param>
private async void FolderBrowserDialog_Open(MOPParameter mopParameter, Int32 rowIndex)
{
    try
    {
        var cellValue = this.dataGridView.Rows[rowIndex].Cells[2].Value?.ToString() ?? string.Empty;
        var folderPicker = new Windows.Storage.Pickers.FolderPicker();
        
        // 文件夹选择必须设置至少一个文件类型过滤
        folderPicker.FileTypeFilter.Add("*");
        folderPicker.CommitButtonText = "选择文件夹";

        // 设置初始路径(如果已有路径)
        if (!string.IsNullOrEmpty(cellValue))
        {
            var initialFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(cellValue);
            folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
        }

        // 绑定当前窗口句柄(确保对话框在应用窗口上层)
        var hwnd = new WindowInteropHelper(this).Handle;
        folderPicker.As<IInitializeWithWindow>().Initialize(hwnd);

        var selectedFolder = await folderPicker.PickSingleFolderAsync();
        if (selectedFolder != null)
        {
            var selectedPath = selectedFolder.Path;
            this.dataGridView.Rows[rowIndex].Cells[2].Value = selectedPath;
            MOPParameterUtil.MOPParameterSetValue(this.mopParameters, mopParameter.ParameterName, selectedPath);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(
            this,
            @"选择文件夹出错。" + Environment.NewLine + e.Message,
            @"配置MOP选项",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
}

注意事项

  • 需要添加Windows.Storage和System.Windows.Interop命名空间引用
  • Windows Forms项目需额外安装Microsoft.Windows.SDK.Contracts NuGet包

方案二:Windows API Code Pack(兼容.NET Framework 4.x)

如果项目基于旧版.NET Framework,可使用Windows API Code Pack提供的CommonOpenFileDialog,它支持快速访问面板且样式更贴近现代对话框。

步骤与代码示例

  1. 安装NuGet包WindowsAPICodePack-Shell
  2. 替换原有文件夹选择逻辑:
/// <summary>
/// 浏览并选择文件夹的事件处理方法
/// </summary>
/// <param name="mopParameter">参数对象</param>
/// <param name="rowIndex">DataGridView中正在编辑的行索引</param>
private void FolderBrowserDialog_Open(MOPParameter mopParameter, Int32 rowIndex)
{
    try
    {
        var cellValue = this.dataGridView.Rows[rowIndex].Cells[2].Value?.ToString() ?? string.Empty;
        using var dialog = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
        
        dialog.IsFolderPicker = true;
        dialog.InitialDirectory = cellValue;
        dialog.Title = "选择文件夹";
        dialog.ShowPlacesList = true; // 启用左侧位置面板,包含快速访问、桌面等区域
        
        if (dialog.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
        {
            var selectedPath = dialog.FileName;
            this.dataGridView.Rows[rowIndex].Cells[2].Value = selectedPath;
            MOPParameterUtil.MOPParameterSetValue(this.mopParameters, mopParameter.ParameterName, selectedPath);
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(
            this,
            @"选择文件夹出错。" + Environment.NewLine + e.Message,
            @"配置MOP选项",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
    }
}

说明

ShowPlacesList = true会开启左侧位置面板,其中包含你需要的快速访问区域,和OpenFileDialog的布局一致。

内容的提问来源于stack exchange,提问作者Bruce B Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 08:55:15