【问题标题】:folder path when it has no files using OpenFileDialog in WPF?在WPF中使用OpenFileDialog没有文件时的文件夹路径?
【发布时间】:2015-07-24 11:05:44
【问题描述】:

如果文件夹中没有文件,如何使用 WPF 中的 OpenFileDialog 类获取文件夹路径

{
      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);
      }
}

但是当没有文件可供选择时,它无法获取文件夹路径

【问题讨论】:

    标签: .net wpf c#-4.0 wpf-controls wpf-4.0


    【解决方案1】:

    如果没有选择文件,ShowDialog() 将不可能返回 true

    如果您首先将OpenFileDialogCheckFileExists 属性设置为true,那么用户可以输入一个不存在的文件名,然后您将获得文件路径。

    我认为您需要的是FolderBrowserDialog,它不是 WPF 内置的,但可以像在这个库中那样实现: wpfdialogs

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 1970-01-01
      • 2013-05-15
      • 2011-11-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-10
      相关资源
      最近更新 更多