Question

On a file path field, I want to capture the directory path like:

textbox1.Text = directory path

Anyone?

Was it helpful?

Solution 2

Well I am using VS 2008 SP1. This all I need:

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

OTHER TIPS

There is a FolderBrowserDialog class that you can use if you want the user to select a folder.

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();
}

If all you want is to get the direcotory from a full path, you can do this:

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

This will set the Text-property to "c:\windows\temp\"

If you don't want a terrible, non-user friendly dialog*, try Ookii.Dialogs or see other answers to How do you configure an OpenFileDialog to select folders?. The only downside I see to Ookii is that it requires .NET 4 Full, not just Client Profile. But the source is included in the download, so I'm going to work on that. Too bad the license isn't LGPL or similar...

See also: WinForms message box with textual buttons

*This is what FolderBrowserDialog looks like:

Ugly, unfriendly folder browser dialog

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top