2

how to get the folder path if no file is present in that folder using OpenFileDialog Class in WPF

{
      Microsoft.Win32.OpenFileDialog dlgObjDest = new Microsoft.Win32.OpenFileDialog();
      dlgObjDest.Multiselect = true;
      dlgObjDest.DefaultExt = ".*";
      dlgObjDest.InitialDirectory = "c:";
      if (dlgObjDest.ShowDialog() == true)
      {
          txtDestTesting.Text = System.IO.Path.GetDirectoryName(dlgObjDest.FileName);
      }
}

but it cant get folder path when there is no file to select

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
Ravindra Nadh
  • 131
  • 2
  • 10

1 Answers1

2

It would be impossible for the ShowDialog() to return true if no file has been selected.

If you set the OpenFileDialog's CheckFileExists property to true first then the user can enter a file name that does not exist and then you would get the file path.

I think what you need is a FolderBrowserDialog, which is not built-in with WPF, but can be implemented like in this library: wpfdialogs

demonplus
  • 5,613
  • 12
  • 49
  • 68
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65