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

3

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

textbox1.Text = directory path

有人吗?
3个回答

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属性设置为"c:\windows\temp\"


1
哦,我以为这是一个好的解决方案,直到我意识到这显示的是哪个对话框 - 我讨厌那个对话框!http://i.imgur.com/2uGPK.png - Pat

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";
    }
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接