Friday, September 7, 2018

Browse for a file in a WinForms application.

Drag OpenFileDialog and FolderBrowserDialog out of the Dialogs section in the Toolbox in Visual Studio 2017. Use the OpenFileDialog to find a file like so:

private void inButton_Click(object sender, EventArgs e)
{
   DialogResult result = openFileDialog.ShowDialog();
   if (result == DialogResult.OK)
   {
      string whereAmI = openFileDialog.FileName;
      Console.WriteLine(whereAmI);
   }
}

 
 

Use a FolderBrowserDialog to find a folder like so:

private void outButton_Click(object sender, EventArgs e)
{
   DialogResult result = folderBrowserDialog.ShowDialog();
   if (result == DialogResult.OK)
   {
      string whereAmI = folderBrowserDialog.SelectedPath;
      Console.WriteLine(whereAmI);
   }
}

 
 

Addendum 9/13/2018: The folder path with NOT have a trailing backslash on it.

No comments:

Post a Comment