February 9, 2010

Dialog To Browse For A Folder

Need these refences
using System.Windows.Forms;
using System.IO;
Sample button logic
  
private void butBrowse_Click(object sender, RoutedEventArgs e)
{
  tbRootDir.Text = BrowseForFolder("Browse to root directory of source", tbRootDir.Text);
}
Browser dialog usage:
private string BrowseForFolder(string descr, string dir)
{
  using (FolderBrowserDialog fbd = new FolderBrowserDialog())
  {
    fbd.Description = descr;
    if (Directory.Exists(dir))
      fbd.SelectedPath = dir;
    if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      if (Directory.Exists(fbd.SelectedPath))
      {
        dir = fbd.SelectedPath;
      }
    }
  }
  return dir;
}

No comments: