【问题标题】:How do a make this type of select folder dialog in C#?如何在 C# 中制作这种类型的选择文件夹对话框?
【发布时间】:2015-04-28 10:58:08
【问题描述】:

所以我最近尝试了FolderBrowserDialog,但令我失望的是它不像下面的截图:

但是它被格式化了,我认为很难像这样导航:

我将如何获得另一个版本,它是一个对话框,询问要保存哪个文件夹,以像本机选择文件类型一样,而不是我认为这个难以导航的菜单。

【问题讨论】:

  • 你使用的是winforms还是wpf?
  • AFAIK FolderBrowserDialog 曾经只是第二个屏幕截图。如果你想要不同的东西,你必须自己动手。
  • 说实话,我不知道我在用什么。我对此很陌生,这可能是我第三次或第四次用 C# 编程......事实上这是我第一次不得不使用调用。
  • 请显示一些代码,因为您不应该使用调用来获取 C# 中的 OpenXXDialog
  • @BerndLinde OP 无法使用 Invoke,所需的文件夹实际上是 Vista 风格的文件对话框,它在 .NET 中不可用。事实上,使用原生调用解决这个问题的方法之一。

标签: c# .net visual-studio-2013


【解决方案1】:

NuGet 包中的 CommonOpenFileDialog“Microsoft.WindowsAPICodePack-Shell” 将回答您的请求。

将 IsFolderPicker 属性设置为 true 就是这样。

using Microsoft.WindowsAPICodePack.Dialogs;     

private bool SelectFolder(out string fileName)
{
    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        fileName = dialog.FileName;
        return true;
    }
    else
    {
        fileName = "";
        return false;
    }
}

【讨论】:

  • like the select file type natively 来自原始问题,但无论如何感谢您的回答,这肯定会帮助其他人。这需要一个 NuGet 包,并在项目中包含一个东西而不是本机方法。
【解决方案2】:

那是因为您使用的是 FolderBrowserDialog 而不是 OpenFileDialog

您可以检查以下内容

 private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Title = "Browse File";
            fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fileDialog.FilterIndex = 2;
            fileDialog.InitialDirectory = "c:\\";
            fileDialog.RestoreDirectory = true;

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                txtFileName.Text = fileDialog.FileName;
            }
        }

【讨论】:

  • 这似乎选择了一个文件,其余的复制粘贴代码似乎从.txt文件中重用,但更改为所有文件*.*
  • 您可以从中提取文件夹名称。文件夹选择未提供您需要的界面检查此链接stackoverflow.com/questions/9227917/…
  • txtFileName 表明这可能是从代码中选择txt 文件类型。我还在做研究,我可能会在 Windows 中找到另一种方法来解决这个问题,因为我不想添加外部包。
  • 实际上 txtFileName 是一个文本框控件,我只是在它们前面加上 txt :)。如果您找到一种方法来完成您需要的事情,请在此处发布作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 2017-11-30
  • 2011-04-29
  • 2015-11-29
  • 1970-01-01
  • 2021-03-05
  • 2017-10-21
相关资源
最近更新 更多