2

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!

Kevdog777
  • 908
  • 7
  • 20
  • 43

2 Answers2

4

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.

Rishabh
  • 3,752
  • 4
  • 47
  • 74
  • Hi Rishabh, yes that is what I am wanting to do. But how do you get it to display in the label / text box? – Kevdog777 Jul 04 '11 at 08:43
  • @Rishabh: put a using around your ofd so it gets disposed as soon as out of scope, to write == true is useless in the if. – Davide Piras Jul 04 '11 at 09:00
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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
  • They didn't say anything about folders vs. files, so why do you think a Folder browser would be more appropriate? – Joey Jul 04 '11 at 08:52
  • @Joey I interpreted `then write out the selected path` to mean he wanted to select a folder, but I see what you are saying. – jonsca Jul 04 '11 at 08:53