打开文件对话框。 “指定目录对话框”怎么样?

时间:2008-09-17 07:33:18

标签: c# directory openfiledialog

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

textbox1.Text = directory path

任何?

3 个答案:

答案 0 :(得分:10)

如果您希望用户选择文件夹,可以使用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 \”

答案 1 :(得分:4)

我正在使用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";
    }
}

答案 2 :(得分:0)

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

另请参阅:WinForms message box with textual buttons

*这就是FolderBrowserDialog的样子:

Ugly, unfriendly folder browser dialog