【问题标题】:OpenFileDialog. How about "Specify Directory Dialog"?打开文件对话框。 “指定目录对话框”怎么样?
【发布时间】:2008-09-17 07:33:18
【问题描述】:

在文件路径字段上,我想捕获目录路径,例如:

textbox1.Text = directory path

有人吗?

【问题讨论】:

    标签: c# directory openfiledialog


    【解决方案1】:

    如果您希望用户选择文件夹,可以使用 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-property 设置为 "c:\windows\temp\"

    【讨论】:

    • 哦,伙计,我认为这是一个很好的解决方案,直到我意识到这显示了哪个对话框 - 我讨厌那个对话框! http://i.imgur.com/2uGPK.png
    【解决方案2】:

    我正在使用 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】:

      如果您不想要一个糟糕的、非用户友好的对话*,请尝试Ookii.Dialogs 或查看How do you configure an OpenFileDialog to select folders? 的其他答案。我看到 Ookii 的唯一缺点是它需要 .NET 4 Full,而不仅仅是客户端配置文件。但是源代码包含在下载中,所以我将继续努力。可惜许可证不是 LGPL 或类似的......

      另见:WinForms message box with textual buttons

      *这是 FolderBrowserDialog 的样子:

      【讨论】:

        猜你喜欢
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多