3

I have one browse button and one text box. In the browse_button click event, I would like to browse the files and place the path name into textbox. For this I've written code like this by using openfile dialog.

private void brwsbtn_Click(object sender, EventArgs e)
        {
            if (openFD.ShowDialog() == DialogResult.OK)
            {
                  textBox1.Text = openFD.FileName;
            }
            textBox1.Text="";
        }

So that I am able to select files only. How can I select and place the folders path in textbox?.

In my application the user should able to select either file or folder through a single browse button. Please suggest me how to write code for this.

Note. Please let me know can we use to upload file without using Openfiledialog in windows form..

4
  • OpenFileDialog is for selecting files, not folders. If you need to select a Folder then there is the FolderBrowserDialog Commented Mar 2, 2016 at 13:36
  • Here i am using excel file only. i am able to select files only but i cant place the folders path in textbox. while clicking the upload button in win form Commented Mar 2, 2016 at 13:39
  • You must use 2 button, one for brow file, one for folder. With the file dialog if you want extract the folder path it is easy. Commented Mar 2, 2016 at 13:47
  • I want to extract file path alone in text box. Commented Mar 2, 2016 at 13:56

2 Answers 2

3

Your code don't add file path to the text box because you have this line:

textBox1.Text = "";

Which auto clear the line:

textBox1.Text = openFD.FileName;

Remove it and you can add file path to textbox:

private void brwsbtn_Click(object sender, EventArgs e)
{
    if (openFD.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = openFD.FileName;
    }
}

If you want file name only (not include path), you can use:

private void brwsbtn_Click(object sender, EventArgs e)
{
    if (openFD.ShowDialog() == DialogResult.OK)
    {
        textBox1.Text = Path.GetFileName(openFD.FileName);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Add a FolderBrowserDialog to your form. Then something like this will work:

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
   {
      textBox1.Text = folderBrowserDialog1.SelectedPath
   }

You can also use your existing fileDialog to do

Path.GetDirectoryName(openFD.FileName);

2 Comments

hello i have to get file path only in texbox not folder path. please kindly guide me
Yes so using your existing code just try this Path.GetDirectoryName(openFD.FileName); or replace your line textBox1.Text = openFD.FileName; with: textBox1.Text = Path.GetDirectoryName(openFD.FileName); also add Using System.IO for usage of Path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.