Create a "file Open" Dialog Button and Write Out to Text Box

16,043

Solution 1

You can use the file open dialog

The file open dialog on success returns the path of the file which is selected, you can then use the returned path and show it on the label.

OpenFileDialog ofd = new OpenFileDialog();

if (ofd.ShowDialog() == true)
{
string filePath = ofd.FileName;
string safeFilePath = ofd.SafeFileName;
}

The string will have the file path assign it to the label.

Solution 2

Assuming your solution is WinForms, and your user is selecting a directory (I'm not sure how to interpret your use of path -- the file's path or a path to a directory), a FolderBrowserDialog might be more appropriate than a OpenFileDialog, as it allows you to choose the folder directly.

Using the FolderBrowserDialog, you can write the SelectedPath propery, which is a string, to your TextBox's .Text property.

If you are trying to determine the path of a specific file, then the OpenFileDialog will work.

Share:
16,043
Kevdog777
Author by

Kevdog777

If I told you about me, then I wouldn't be so mysterious!

Updated on June 05, 2022

Comments

  • Kevdog777
    Kevdog777 about 2 years

    I am wanting to create a browse (fileOpen Dialog) button to search my local drive and then write out the selected path to a text field.

    I am using Visual Studio Express 2010

    Any help much appreciated!