uid
uid

Reputation: 347

How I can make a button for browse a folder?

I try to search a folder and when I find to copy the address in a textBox1 .I have the next code, but this doesn't work properly, with this code I just find the files. My question is : How can I change the code to make a browse button for find a folder and when I find to copy the address in textBox1?

private void browse_Click(object sender, EventArgs e) 
{
    OpenFileDialog fDialog = new OpenFileDialog();
    fDialog.Title = "Browse";
    fDialog.InitialDirectory = @"C:\LegacyApp\MATLAB\R2008a_64-bit";
    fDialog.Filter = "All files(*.*)|*.*|All files(*.*)|*.*";
    fDialog.FilterIndex = 2;
    fDialog.RestoreDirectory = true;
    if (fDialog.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = fDialog.FileName;               
    }
}

Upvotes: 3

Views: 3311

Answers (3)

Ajar
Ajar

Reputation: 162

I have tried like follows, Its working for me.

private void browse_Click(object sender, EventArgs e) { var fDialog = new OpenFileDialog { Title = "Browse", InitialDirectory = @"C:\LegacyApp\MATLAB\R2008a_64-bit", Filter = "All files(*.*)|*.*|All files(*.*)|*.*", FilterIndex = 2, RestoreDirectory = true }; if (fDialog.ShowDialog() == DialogResult.OK) { textBox1.Text = fDialog.FileName; } }

Upvotes: 0

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

To browse folder, you need FolderBrowserDialog

private void browse_Click(object sender, EventArgs e) 
{
     if (folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
     {
         textBox1.Text = folderBrowserDialog1.SelectedPath;
     }
}

Upvotes: 3

Nikerym
Nikerym

Reputation: 515

Try something like the following

textbox1.Text = fdialog.FileName.Substring(0, fdialog.FileName.lastIndexOf(@"\"));

Upvotes: 1

Related Questions