代码之家  ›  专栏  ›  技术社区  ›  MarlonRibunal

OpenFileDialog。“指定目录对话框”怎么样?

  •  3
  • MarlonRibunal  · 技术社区  · 17 年前

    在文件路径字段中,我想捕获目录路径,如下所示:

    textbox1.Text = directory path
    

    有人吗?

    3 回复  |  直到 14 年前
        1
  •  10
  •   Espo    17 年前

    如果希望用户选择文件夹,可以使用FolderBrowserDialog类。

    http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx

    DialogResult result = folderBrowserDialog1.ShowDialog();
    if (result.Equals(get_DialogResult().OK)) {
        textbox1.Text = folderBrowserDialog1.get_SelectedPath();
    }
    

    如果你只想从完整的路径中获取目录故事,你可以这样做:

    textbox1.Text = Path.GetDirectoryName(@"c:\windows\temp\myfile.txt");
    

    这将把Text属性设置为“c:\windows\temp\”

        2
  •  4
  •   Mong Zhu Bart de Boer    10 年前

    我使用的是VS 2008 SP1。这就是我所需要的:

    private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog profilePath = new FolderBrowserDialog();
    
        if (profilePath.ShowDialog() == DialogResult.OK)        
        {
            profilePathTextBox.Text = profilePath.SelectedPath;
        }
        else
        {
            profilePathTextBox.Text = "Please Specify The Profile Path";
        }
    }
    
        3
  •  1
  •   Community Mohan Dere    9 年前

    如果你不想看到一个糟糕的、非用户友好的对话框,请尝试 Ookii.Dialogs 或查看其他答案 How do you configure an OpenFileDialog to select folders? 我认为Ookii唯一的缺点是它需要。NET 4完整版,而不仅仅是客户端配置文件。但是源代码包含在下载中,所以我将对此进行研究。遗憾的是,该许可证不是LGPL或类似许可证。..

    另请参见: WinForms message box with textual buttons

    *这就是FolderBrowserDialog的样子:

    Ugly, unfriendly folder browser dialog