【问题标题】:How to choose a folder without opening it with OpenFileDialog C#如何在不使用 OpenFileDialog C# 打开的情况下选择文件夹
【发布时间】:2017-10-19 07:59:58
【问题描述】:

我必须创建一个按钮,用户必须使用该按钮选择一个文件夹。 我已经尝试使用 OpenFileDialog,但是我无法选择文件夹并且文件夹刚刚打开。

这是我的代码:

 private void button2_Click(object sender, EventArgs e)
        {

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = @"D:\dosfiles\ValPoch";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;

            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                label2.Text = fdlg.FileName;
                label2.Show();
            }

我用这段代码试试,他工作得很好,但是我不喜欢那个打开的窗口,太小了。

using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                dlg.Description = "Select a folder";
                dlg.SelectedPath = @"D:\dosfiles\ValPoch\";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    label2.Text = dlg.SelectedPath;
                    label2.ForeColor = Color.Red;
                    label2.Show();
                }
            }

如何使用 OpenFileDIalog 修复我的代码以选择不打开该文件夹的文件夹?

谢谢。

【问题讨论】:

  • 我不想打开这个小窗口来选择文件夹。我postet时的第二个代码相同,但是打开选择文件夹的窗口太小,与我的第一个代码不同。
  • OpenFileDialog 和 FolderBrowseDialog 有不同的窗口..
  • 你能通过区分“打开文件夹”和“选择文件夹”更清楚地解释你的意思吗?
  • 请看我之前粘贴的链接中的第二个答案。我想这就是你想要的吗?

标签: c# visual-studio windows-forms-designer openfiledialog


【解决方案1】:

看看Ookii.Dialogs,这是一个很棒的库,可以使用常见的 Windows 对话框,包括文件对话框。

【讨论】:

    猜你喜欢
    • 2023-03-24
    • 2021-12-27
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多