Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio file selector

I am new to .NET and the Visual Studio IDE. How to I add to a form a component that lets the user browse the file system to select a certain file which will then allow me to programmatically use the selected path as a string variable?

like image 468
kaj Avatar asked Nov 04 '09 00:11

kaj


2 Answers

this should do the trick:

string path;
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK)
{
    path = file.FileName;
}

the string path should now contain the selected file path

**Edit: ** As mentioned in a comment below, OpenFileDialog is disposable so should be wrapped in a using statement.

like image 180
harryovers Avatar answered Sep 22 '22 05:09

harryovers


OpenFileDialog should suit your needs. You'll probably need to put a button (or some other clickable type UI element) on the page that will pop the dialog up. Then once the user has selected a file and clicked "OK" you'll just check the response for which file was selected.

like image 31
Gregg Avatar answered Sep 22 '22 05:09

Gregg